RubyCocoa: Thumbnail Images in NSTableView - cocoa

I'm trying to display an NSTableView of:
| thumbnail | filename |
I created the NSTableView in IB and delegated it to a class. In the class, I bypassed a model just to get a POC implementation and created the data source delegate methods. They populate the text cells just fine. However, now I'm at the step where the first cell needs to contain a small thumbnail of the image.
What I want to do (and I'm sure it's stupid-simple) is grab the image icon -- and it's fair to assume that all files are jpegs and have thumbnails embedded -- and put that icon, scaled to 64x64 into the table cell. There's lots of code on how to generate thumbnails but I don't see much that gets me to working code. Here's what I have:
# This works if I am only populating text values in the when 'Image'
def tableView_objectValueForTableColumn_row_(image_table, column, row)
thumbnailImage(75)
case column.headerCell.stringValue
when 'File Name'
(0..99).to_a[row].to_s
when 'Image'
# here's where I want to return a square 64x64 image or ImageCell
thumbnailImage(64)
else
'???'
end
end
# Creates square thumbnail
def thumbnailImage(size)
file = "file://localhost/Users/sxross/Downloads/iStock_000004561564XSmall.jpg"
image = CGImageSourceCreateWithURL(CFURLCreateWithString(nil, file, nil), nil)
thumb = CGImageSourceCreateThumbnailAtIndex(image, 0, nil)
thumb
end
def numberOfRowsInTableView(view)
100
end
What I'm grappling with is what the missing steps are to get the thumbnailImage method to provide me with something that can be an appropriate data object to return from the data source.
Any help is amazingly appreciated.
BTW: IknowIknowIknow, I should be using MacRuby but it doesn't run on my 32-bit Core Duo. Sadly.

I bypassed a model …
Don't do that. In Cocoa, it's easier to do things with a model than without.
What I'm grappling with is what the missing steps are to get the thumbnailImage method to provide me with something that can be an appropriate data object to return from the data source.
CGImageSourceCreateThumbnailAtIndex returns a CGImage. You need tableView_objectValueForTableColumn_row_ to return an NSImage. Therefore, use NSImage's initWithCGImage_size_ method.
If you're running Leopard, that method isn't available, so you'll need to create an NSBitmapImageRep with the CGImage instead, and then create an NSImage of the correct size and add that representation to it.

Related

Liquid Pixels check source image mime type

unfortunately, I cannot tag this post with the correct "technology" because it does not exist and i dont have 1500 reputation to create it.
We are using a cloud service called "Liquid Pixels" to render some stuff on our images.
Lets say we have an image chain that is currently rendering a ribbon on the given JPEG image. This chain is working fine.
Then I adapted the chain to work with animated gif images, therefore I changed the sink format to gif (sink=format[gif]). That was working fine as well.
Now I want to combine the two cases in one chain, because the only difference is the sink command. The plan is to check the MIME type of the source image and then either render a gif or a jpg image.
I rendered the image as xml to view the metadata map.
I thought i can do it like this.
source=url[https://something.com/1x1_sample.gif],name[testImage]
sink=format[gif],if[('testImage.format' eq 'GIF')]
sink=format[jpg],if[('testImage.format' ne 'GIF’)]
But for some reason I cannot access the format attribute. I am used to grab some parameters like “testImage.width” or “testImage.height”, but for some reason i cannot access the format=“GIF” property. I guess that has happens because the width and height are on a different hierarchy level in the metadata map.
I hope you guys can help me.
The image does not actually have a "format" during the render. Only a file has a format. During processing the image is simply on memory as either raster or vector data; it is only when you sink that it becomes a file in whatever format. Also, LiquiFire OS uses the image data to determine the original format when acquiring an image from a source, never the image name itself.
If you need operations in your LiquiFire Image Chain to react to the source image URL, you can test the last part of the image name by applying a regular expression to see if it is either .GIF or .gif. An example of how that can be done:
set=imageURL[https://your.server.com/sample.gif]
source=url[global.imageURL],name[testImage]
regexcase=name[isGif],key[global.imageURL],cases[\.gif$|\.GIF$|\.\w+$],values[yes|yes|no]
sink=format[gif],if[('global.isGif' eq 'yes')]
sink=format[jpg],if[('global.isGif' eq 'no’)]

How to bind an image used in a mask of a simulink block to said block?

To clarify this at the beginning:
With image inside the mask of subsystem i do NOT mean an image drawn onto the block, but rather an image one can add to the mask:
Is there a way to bind the image to the block? In case I want to distribute my model, I don't want to have to share every image used in it.
For an image drawn onto the block I found a solution here, that is storing the image inside the UserData of the block, but I can't find an option to change the properties of images used inside a mask.
This might be a bit too late, but having the same problem I 'fixed' it by including the image and its alpha values in the 'UserData' parameter, checking if the image already exists in the current folder, and if not creating it from the userdata:
if ~exist('ARMicon.png','file')
maskParams = Simulink.Mask.get(gcb);
armim = maskParams.getDialogControl('armPic');
ud = get_param(gcb,'UserData');
imwrite(ud.ARM,'ARMicon.png','Alpha',ud.alpha);
armim.FilePath = 'ARMicon.png';
end
Hope this helps.
Quote out of my correspondence with the MathWorks Technical Support:
Unfortunately, it is currently not possible to specify a mask dialog image without providing a file path and a separate image file. This has been brought to the attention of the development team as a possible enhancement for a future release.
This refers to Matlab / Simulink 9 (2016a).

How to access a class resource in an instance

I am trying to make a simple game, so far i can capture user input, but i cannot get the view to work properly to print the images. If i have a resource named image, how do i assign individual sprites to display this image on displayOn? I have tried many approaches, for example in the initialize method i tried:
self image := Classname image
but that caused an overflow, and i was forced to closed visual without saving work.
What is the good way to do this?
you usually access to class side methods directly, without needing to store there into instance variables. For example:
myMethodsWhoNeedsAnImage
| image |
image := self class imageStoredInClassSide.
"now do something with image"
If you need to store it, certainly you cannot do what you tried in your example, but you can do:
initialize
super initialize.
image := ClassWithImage image.
or
initialize
super initialize.
self image: ClassWithImage image. "This is a setter method"
Any of these approaches should work. If it doesn't, most probably you have a problem somewhere else, not in the accessing to class side.

Trying to turn [NSImage imageNamed:NSImageNameUser] into NSData

If I create an NSImage via something like:
NSImage *icon = [NSImage imageNamed:NSImageNameUser];
it only has one representation, a NSCoreUIImageRep which seems to be a private class.
I'd like to archive this image as an NSData but if I ask for the TIFFRepresentation I get a
small icon when the real NSImage I originally created seemed to be vector and would scale up to fill my image views nicely.
I was kinda hoping images made this way would have a NSPDFImageRep I could use.
Any ideas how can I get an NSData (pref the vector version or at worse a large scale bitmap version) of this NSImage?
UPDATE
Spoke with some people on Twitter and they suggested that the real source of these images are multi resolution icns files (probably not vector at all). I couldn't find the location of these on disk but interesting to hear none-the-less.
Additionally they suggested I create the system NSImage and manually render it into a high res NSImage of my own. I'm doing this now and it's working for my needs. My code:
+ (NSImage *)pt_businessDefaultIcon
{
// Draws NSImageNameUser into a rendered bitmap.
// We do this because trying to create an NSData from
// [NSImage imageNamed:NSImageNameUser] directly results in a 32x32 image.
NSImage *icon = [NSImage imageNamed:NSImageNameUser];
NSImage *renderedIcon = [[NSImage alloc] initWithSize:CGSizeMake(PTAdditionsBusinessDefaultIconSize, PTAdditionsBusinessDefaultIconSize)];
[renderedIcon lockFocus];
NSRect inRect = NSMakeRect(0, 0, PTAdditionsBusinessDefaultIconSize, PTAdditionsBusinessDefaultIconSize);
NSRect fromRect = NSMakeRect(0, 0, icon.size.width, icon.size.width);;
[icon drawInRect:inRect fromRect:fromRect operation:NSCompositeCopy fraction:1.0];
[renderedIcon unlockFocus];
return renderedIcon;
}
(Tried to post this as my answer but I don't have enough reputation?)
You seem to be ignoring the documentation. Both of your major questions are answered there. The Cocoa Drawing Guide (companion guide linked from the NSImage API reference) has an Images section you really need to read thoroughly and refer to any time you have rep/caching/sizing/quality issues.
...if I ask for the TIFFRepresentation I get a small icon when the
real NSImage I originally created seemed to be vector and would scale
up to fill my image views nicely.
Relevant subsections of the Images section for this question are: How an Image Representation is Chosen, Images and Caching, and Image Size and Resolution. By default, the -cacheMode for a TIFF image "Behaves as if the NSImageCacheBySize setting were in effect." Also, for in-memory scaling/sizing operations, -imageInterpolation is important: "Table 6-4 lists the available interpolation settings." and "NSImageInterpolationHigh - Slower, higher-quality interpolation."
I'm fairly certain this applies to a named system image as well as any other.
I was kinda hoping images made [ by loading an image from disk ] would
have a NSPDFImageRep I could use.
Relevant subsection: Image Representations. "...with file-based images, most of the images you create need only a single image representation." and "You might create multiple representations in the following situations, however: For printing, you might want to create a PDF representation or high-resolution bitmap of your image."
You get the representation that suits the loaded image. You must create a PDF representation for a TIFF image, for example. To do so at high resolution, you'll need to refer back to the caching mode so you can get higher-res items.
There are a lot of fine details too numerous to list because of the high number of permutations of images/creation mechanisms/settings/ and what you want to do with it all. My post is meant to be a general guide toward finding the specific information you need for your situation.
For more detail, add specific details: the code you attempted to use, the type of image you're loading or creating -- you seemed to mention two different possibilities in your fourth paragraph -- and what went wrong.
I would guess that the image is "hard wired" into the graphics system somehow, and the NSImage representation of it is merely a number indicating which hard-wired graphic it is. So likely what you need to do is to draw it and then capture the drawing.
Very generally, create a view controller that will render the image, reference the VC's view property to cause the view to be rendered, extract the contentView of the VC, get the contentView.layer, render the layer into a UIGraphics context, get the UIImage from the context, extract whatever representation you want from the UIImage.
(There may be a simpler way, but this is the one I ended up using in one case.)
(And, sigh, I suppose this scheme doesn't preserve scaling either.)

Related to imagelist

I m working in delphi-2010
i have an imagelist,in which i have added some .png images.
and i have also picture for showing the picture from imagelist.
i want to show the picture on picture box from imagelist.
i wrote the following code,but here addImage(,) takes 2 argument
one is Values:TCustomImagelist
and another is Index of Image
how i identify the value of customimagelist.
image1.Picture:=imagelist1.AddImage( , );
TImageList.AddImage adds a single image from one TImageList to another. I don't think this is what you intended.
If you want to show one of the images from a TImageList in a TImage, you could use something like this:
imageList1.GetBitmap(0, image1.Picture.Bitmap);

Resources