Search code examples
macoscocoansdatansimage

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?)


Solution

  • Both of your major questions are answered in 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.