Scaled image in NSImageView looks bad with layer-backing on

醉酒当歌 提交于 2019-12-10 11:38:16

问题


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?


回答1:


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.

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.

Here's code that works for me.

    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];

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.



来源:https://stackoverflow.com/questions/13588865/scaled-image-in-nsimageview-looks-bad-with-layer-backing-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!