Nsimage画在rect中,但图像模糊不清?

时间:2016-01-05 06:25:56

标签: macos nsimage drawinrect

设计师给我这样的照片enter image description here

但是当我使用drawInRect API在上下文中绘制图片时,图片就像这样enter image description here

rect的大小就是图像的大小。图像是@ 1x和@ 2x。

差异非常明显,图像模糊,图像右侧有灰线,而我的imac是视网膜分辨率。

=============================================== = 我找到了原因,

[self.headLeftImage drawInRect:NSMakeRect(100,
                                        100,
                                        self.headLeftImage.size.width,
                                        self.headLeftImage.size.height)];




CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(context);
CGContextTranslateCTM(context, self.center.x , self.center.y);

[self.headLeftImage drawInRect:NSMakeRect(100,
                                        100,
                                        self.headLeftImage.size.width,
                                        self.headLeftImage.size.height)];
CGContextRestoreGState(context);

在第一次绘制时,图像不会模糊,但翻译后图像模糊。就像图片:enter image description here

1 个答案:

答案 0 :(得分:1)

问题在于您将上下文转换为非整数像素位置。然后,绘图是为了表示您将图像置于非整数位置的请求,这会使其在某些像素中部分消除锯齿和颜色。

您应该将中心点转换为设备空间,对其进行积分(例如,使用floor()),然后将其转换回来。使用CGContextConvertPointToDeviceSpace()CGContextConvertPointToUserSpace()进行转化。这对Retina和非Retina显示器来说是正确的。