【问题标题】:Nsimage draw in rect but the image is blurry?Nsimage 以矩形绘制但图像模糊?
【发布时间】:2016-04-08 21:49:48
【问题描述】:

设计师给我这样的图片

但是当我使用drawInRect API在上下文中绘制图片时,图片是这样的

矩形的大小就是图片的大小。图片是@1x和@2x。

区别很明显,图片模糊,图片右侧有一条灰线,我的imac是retina分辨率。

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

[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);

并且在第一次绘制时图像不会模糊,但在平移后图像会模糊。如图:

【问题讨论】:

  • 实际应用中图片的类型是什么?它是位图还是矢量格式?您为绘图调用提供了什么矩形?
  • 我已经编辑了我的描述,谢谢。
  • @Ken 类型是png~

标签: macos nsimage drawinrect


【解决方案1】:

问题在于您将上下文转换为非整数像素位置。然后,抽奖会尊重您将图像放置在非整数位置的请求,这会导致它被消除锯齿并在某些像素中部分着色。

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

【讨论】:

  • 感谢您的帮助,我只知道我应该单击检查以标记已接受的答案。
猜你喜欢
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
相关资源
最近更新 更多