问题描述
我想要一个带有渐变背景和图像的视图.因此,我为该图层打开了图层支持,添加了NSImageView
并将图层设置为CAGradientLayer
.但是,这使按比例缩小的图像非常难看.查看比较:
I'd like to have a view with a gradient background and a image. So I turned on layer backing for the layer, added NSImageView
and set the layer to CAGradientLayer
. However, this makes the scaled-down image very ugly. See the comparison:
图层开启:,图层关闭:
Layer ON: , layer OFF:
有人也观察到过吗?有什么方法可以在不关闭图层支持的情况下解决此问题?
Has anyone observed this too? Is there any way to resolve this problem without turning off layer backing?
推荐答案
在遇到完全相同的问题后,我偶然发现了这个问题.一层支持NSImageView的渲染看起来非常缩放,但是没有一层支持的NSImageView看起来很棒.
I stumbled across this question after running into exactly the same problem. The rendering for a layer-backed NSImageView looked horribly scaled, but non-layer-backed one looked great.
我尝试了许多不同的方法,但最终,实际上是来自我当地CocoaHeads小组的人的建议,最终导致了解决方案.
I tried a number of different things, but in the end it was actually a suggestion from people at my local CocoaHeads group that ultimately led to a solution.
这是对我有用的代码.
CGImageRef imageRef;
NSRect rect;
rect = NSZeroRect;
rect.size = [_imageView bounds].size;
imageRef = [image CGImageForProposedRect:&rect
context:[NSGraphicsContext currentContext]
hints:@{ NSImageHintInterpolation: @(NSImageInterpolationHigh)}];
image = [[[NSImage alloc] initWithCGImage:imageRef size:rect.size] autorelease];
[_imageView setImage:image];
起初,我只是假设插值模式是问题所在.但是,更改该值似乎无济于事.关于从NSImage-> CGImage-> NSImage进行的操作是什么.我讨厌提供一个我不完全了解的解决方案,但至少是这样.
At first, I'd just assumed that the interpolation mode was the problem. But, changing that value does not appear to make a difference. Something about going from NSImage->CGImage->NSImage is what does it. I hate to offer a solution I do not fully understand, but at least it's something.
这篇关于启用图层支持后,NSImageView中的缩放图像看起来很糟糕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!