本文介绍了在CALayer中自动缩放多页TIFF NSImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我有一个多页TIFF图像(用tiffutil生成),包含多个像素尺寸的相同图像,从256x128像素到4096x2048像素。我想在 CALayer 中显示此图像,以便系统根据图层的大小自动选择图像的最佳表示。目前,图层始终使用图片的256x128表示形式,而不管其大小。

Problem: I have a multipage TIFF image (generated with tiffutil) that contains the same image at multiple pixel dimension from 256x128 px all the way up to 4096x2048 px. I want to display this image in a CALayer so that the system automatically chooses the best representation of the image depending on the layer's size. At the moment, the layer always uses the 256x128 representation of the image, regardless of its size.

这里是我做的:我用

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

记录映像具有不同像素大小的多个表示,但是所有表示在点(256×128)中具有相同的大小。 AFAIK这是Apple建议多重分辨率图片的构建方式。

Logging the image object confirms that it contains multiple representations with different pixel sizes, but all representations are the same size in points (256x128). AFAIK this is how Apple recommends multi-resolution images to be constructed.

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

然后直接将 NSImage 图层的 contents 属性:

I then assign the NSImage instance directly to the layer's contents property:

self.layerView.layer.contents = image;

如上所述,结果是图层使用第一个表示形式(256x128 px)

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

当我将同一个图片分配给 NSImageView 时,按预期工作。图像视图根据其大小透明地选择最佳图像表示。我会期望 CALayer 将工作方式相同,但显然不是这样。任何人都可以确认 CALayer 不支持此自动选择或我做错了什么?

When I assign the same image to an NSImageView, it works as expected. The image view transparently selects the best image representation depending on its size. I would expect that CALayer would work the same way but apparently this is not the case. Can anybody confirm that CALayer does not support this automatic selection or am I doing something wrong?

问题与HiDPI / Retina图形不直接相关,事实上,如果我在HiDPI模式下将图层移动到显示,它会渲染得更清晰,表示它现在使用第二个位图表示(512x256 px)进行渲染。建议在HiDPI显示器上选择更高分辨率的自动操作工作,而最佳位图表示的基本选择失败。)

(Note that this question is not directly related to HiDPI/Retina graphics. In fact, if I move the layer to a display in HiDPI mode, it does render a little sharper, indicating that it now uses the second bitmap representation (512x256 px) for rendering. This suggests that the automatism to select a higher resolution on a HiDPI display works while the fundamental selection of the best bitmap representation fails.)

推荐答案

看起来像AppKit方法 - [CALayer setContents:] 选择大小匹配的位图表示 - [content size] 如果 contents 对象是 NSImage 。然后,所选择的位图被使用,直到 - [CALayer setContents:] 被再次调用。

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

这篇关于在CALayer中自动缩放多页TIFF NSImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 21:43