Scaled image in NSImageView looks bad with layer-b

2019-06-08 10:04发布

问题:

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.