Trying to turn [NSImage imageNamed: NSImageNameUser] into NSData

If I create NSImage through something like:

 NSImage *icon = [NSImage imageNamed:NSImageNameUser]; 

it has only one view, a NSCoreUIImageRep , which seems private.

I would like to archive this image as NSData , but if I ask for TIFFRepresentation , I will get a small icon when the real NSImage that I originally created was apparently a vector and scaled to fit my images well.

I kind of hoped that images made this way would have an NSPDFImageRep that I could use.

Any ideas how I can get the NSData (vector version prefix or, in the worst case, large-scale version of the bitmap) of this NSImage ?

UPDATE

We talked with some people on Twitter , and they suggested that the real source of these images are icns files with several resolutions (probably not a vector), I could not find their location on the disk, but it’s interesting to hear no less.

In addition, they proposed creating an NSImage system and manually turning it into a high level NSImage. I am doing it now and it works for my needs. My code is:

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

+7
source share
2 answers

You seem to be ignoring the documentation. Answers to both basic questions. Cocoa's Drawing Guide (a companion link guide related to the NSImage API reference) has a "Pictures" section that you really need to read carefully and consult anytime you have problems with rep / caching / calibration / quality.

... if I ask for a TIFF presentation, I get a small icon when the real NSImage that I originally created was vectorial and scalable to fill my images well.

The relevant subsections of the Images section for this issue are: like image, image and image caching, as well as image size and resolution. By default, -cacheMode for the TIFF image "Behaves as if the NSImageCacheBySize parameter was set." In addition, for image scaling / calibration operations, -imageInterpolation is important: "Table 6-4 lists the available interpolation options." and "NSImageInterpolationHigh - Slower, Better Interpolation."

I am sure that this applies to the named sample system, as well as to any other.

I was hoping that the images taken [by loading the image from disk] would have an NSPDFImageRep that I could use.

Corresponding subsection: Representations of images. "... with file images, most of the images you create require only one image representation." and "However, you can create several views in the following situations: for printing, you may need to create a PDF view or a high-resolution bitmap of your image."

Got a view that matches the loaded image. For example, you should create a PDF view for a TIFF image. To do this with high resolution, you need to return to cached mode so that you can get higher resolution items.

Too many small details that are listed due to the large number of permutations of the images / creation mechanisms / settings / and what you want to do with all this. My post is for general guidance on finding the specific information you need for your situation.

For more details, add specific details: the code you tried to use, the type of image you are loading or creating - in your fourth paragraph you mentioned two different possibilities - and what went wrong.

+4
source

I would suggest that the image is “hard-wired” to the graphics system in some way, and the NSImage view is just a number indicating which one has wired graphics. Most likely you need to do this and then capture the drawing.

In general, create a view controller that will display the image, reference the VC view property to force it to render the view, extract the contentView from the VC, get the contentView.layer , map the layer to the UIGraphics context, get the UIImage from the context, extract any view from the UIImage.

(Maybe an easier way, but this is the one I used in one case.)

(And, sigh, I believe that this scheme does not preserve scaling.)

0
source

All Articles