4

I have two PNGs in a Mac project. Normal and @2x. Xcode combines these into a single TIFF with the @2x being at index 0 and the @1x at index 1.

What is the suggested approach to get the appropriate image as CGImageRef version (for use with Quartz) for the current display scale?

I can get the image manually via CGImageSource:

NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *URL = [mainBundle URLForResource:@"Canvas-Bkgd-Tile" withExtension:@"tiff"];

CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)(URL), NULL);
_patternImage = CGImageSourceCreateImageAtIndex(source, 1, NULL);  // index 1 is @1x, index 0 is @2x
CFRelease(source);

I also found this to be working, but I am not certain that this will return the Retina version on a Retina display:

NSImage *patternImage = [NSImage imageNamed:@"Canvas-Bkgd-Tile.tiff"];
_patternImage = [patternImage CGImageForProposedRect:NULL context:nil hints:nil];
CGImageRetain(_patternImage); // retain image, because NSImage goes away

An acceptable answer to this question either provides a solution how to get the CGImage suitable from a combined multi-resolution TIFF, or explains why the second approach here is working. Or what changes are required.

1
  • 1
    Have you tried using [[NSScreen mainScreen] backingScaleFactor] ? Obviously this will only give you the scale factor of the "main screen" (the one with keyboard focus), but might be enough for you to determine which image you need. Alternatively I guess you could iterate over all of the screens and detect whether any of them have a scale factor of > 1.
    – Circumflex
    Jan 22, 2013 at 12:37

1 Answer 1

1

I am opting to answer on "why the second approach here is working".

In one of the WWDC videos published since 2010, they said that :

+[NSImage imageNamed:] chooses the best image representation object available for the current display.

So chances are that you are calling this class method from within a locked focus context (e.g. within a drawRect: method or similar), or maybe you actually called lockFocus yourself. Anyway, the result is that you get the most suitable image. But only when calling +[NSImage imageNamed:].

EDIT: Found it here: http://adcdownload.apple.com//wwdc_2012/wwdc_2012_session_pdfs/session_213__introduction_to_high_resolution_on_os_x.pdf

Search for the keyword "best" in the slides: "NSImage automatically chooses best representation […]".

So, your second version will return the Retina version on a Retina display, you can be certain of it, it is advertised in the documentation[*].

[*] This will only work if you provide valid artwork.

1
  • 2
    @Cocoanetics by valid artwork, I mean that your artwork must contain both normal and retina versions. Then, using +[NSImage imageNamed:] will always load the most suitable representation.
    – Jean
    Feb 24, 2013 at 15:43

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.