Automatically scale multi-page TIFF NSImage in CALayer

Problem: I have a multi-page TIFF image (generated from tiffutil) that contains the same image with a size of several pixels from 256x128 px to 4096x2048 px. I want to display this image in CALayer so that the system automatically selects the best representation of the image depending on the size of the layer. Currently, a layer always uses a 256x128 image representation, regardless of its size.

Here's what I do: upload an image using

 NSImage *image = [NSImage imageNamed:@"map-multipage.tiff"]; 

Recording the image object confirms that it contains several views with different pixel sizes, but all views have the same size in pixels (256x128). AFAIK is how Apple recommends creating images with multiple resolutions.

 NSLog(@"%@", image); <NSImage 0x100623060 Name=map-multipage Size={256, 128} Reps=( "NSBitmapImageRep 0x10064d330 Size={256, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=256x128 Alpha=NO Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x10014fdb0", "NSBitmapImageRep 0x10064e1b0 Size={256, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=512x256 Alpha=NO Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x10014fdb0", ... "NSBitmapImageRep 0x100530bd0 Size={256, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=4096x2048 Alpha=NO Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x10014fdb0" )> 

Then I assign the NSImage instance directly to the contents layer property:

 self.layerView.layer.contents = image; 

As already mentioned, the result is that the layer uses the first view (256x128 px) to display the image, regardless of the size of the layer in pixels or pixels.

When I assign the same NSImageView image, it works as expected. In the image view, the best image is transparently selected based on its size. I would expect CALayer to work the same way, but apparently it is not. Can anyone confirm that CALayer does not support this automatic selection, or am I doing something wrong?

(Note that this question is not directly related to HiDPI / Retina graphics. In fact, if I move the layer to the display in HiDPI mode, it makes it a little sharper, indicating that it now uses the second representation of the bitmap (512x256 px) for rendering This suggests that the automation of choosing a higher resolution on the HiDPI display works when the main choice of a better representation of the bitmap is not obtained.)

+7
source share
1 answer

It looks like the AppKit method -[CALayer setContents:] selects the bitmap representation of the size matching -[contents size] if the contents object is NSImage . Then, the selected bitmap is used as until -[CALayer setContents:] is called again.

+2
source

All Articles