简体   繁体   English

nsimage画入rect,但是图像模糊吗?

[英]Nsimage draw in rect but the image is blurry?

designer give me picture like this 设计师给我这样的照片 在此处输入图片说明

But when I use drawInRect API draw the picture in context, the picture is like this 但是当我使用drawInRect API在上下文中绘制图片时,图片是这样的 在此处输入图片说明

The size of the rect is just the size of the image.And the image is @1x and @2x. 矩形的大小就是图像的大小,图像分别是@ 1x和@ 2x。

the difference is very clear, the picture is blurry and there is a gray line in the right of image, and My imac is retina resolution. 区别非常明显,图像模糊,图像右侧有灰线,而我的imac是视网膜分辨率。

================================================ I have found the reason, ===============================================我有找到了原因,

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

And in the first draw the image will not blur, but after translate the image is blurry. 并且在第一次绘制中图像不会模糊,但是在平移后图像会模糊。 Just like the picture: 就像图片一样: 在此处输入图片说明

The problem is that you're translating the context to a non-integral pixel location. 问题是您要将上下文转换为非整数像素位置。 Then, the draw is honoring your request to put the image at a non-integral position, which causes it to be anti-aliased and color in some pixels partially. 然后,抽签将满足您的要求,即将图像放置在非积分位置,这会使图像抗锯齿,并且在某些像素中会部分着色。

You should convert the center point to device space, integral-ize it (eg by using floor() ), and then convert it back. 您应该将中心点转换为设备空间,对其进行积分(例如,通过使用floor() ),然后再将其转换回去。 Use CGContextConvertPointToDeviceSpace() and CGContextConvertPointToUserSpace() to do the conversions. 使用CGContextConvertPointToDeviceSpace()CGContextConvertPointToUserSpace()进行转换。 That does the right thing for Retina and non-Retina displays. 这对于Retina和非Retina显示器来说是正确的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM