在这种情况下,是否必须将比例乘以视网膜显示的点值? [英] Must I multiply the scale with the point values for retina display, in this case?

查看:59
本文介绍了在这种情况下,是否必须将比例乘以视网膜显示的点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从视网膜显示起,这段绘图代码突然似乎不再起作用。

Since the retina display, suddenly this piece of drawing code seems to not work anymore. The drawn image is slightly offset than it was before and appears somewhat stretched.

我正在UIControl子类的-drawRect:方法中绘制一些内容。我发现UIControl内部的当前比例确实是2.0。这段代码是从UIImage获取CGImage的,它可能对缩放比例一无所知。它作为参数输入到现在也需要一些点值的方法中。

I am drawing something in the -drawRect: method of an UIControl subclass. I figured out that the current scale inside that UIControl indeed is 2.0. This code obtains an CGImage from an UIImage, which probably won't know anything about the scale. It's feeded as parameter to an method that also takes some point values right now.

CGContextDrawImage(context, CGRectMake(drawingRect.origin.x, drawingRect.origin.y, img.size.width, img.size.height), [img CGImage]);

注意:drawingRect以磅为单位。 NSLog内的img.size.width确实输出以磅为单位的正确值,而[img CGImage]确实输出@ 2x图像用于视网膜显示。我做了检查来验证这一点:

Note: drawingRect is in points. img.size.width inside an NSLog does output the correct value in points, while [img CGImage] does output the @2x image for retina display. I did a check to verify this:

NSLog(@"image height = %f (CGImage = %d)", img.size.height, CGImageGetHeight([img CGImage]));

控制台输出:图像高度= 31.000000(CGImage = 62)

在这里如何处理@ 2x图像?我必须手动将每个值乘以比例尺吗?但这还会搞乱屏幕上实际的可见矩形吗?

How would I deal with the @2x image here? Must I multiply every value with the scale, manually? But that would also screw up the actual visible rectangle on the screen, or not?

推荐答案

是。

CGImageGetWidth([image CGImage]) == image.size.width * image.scale
CGImageGetHeight([image CGImage]) == image.size.height * image.scale

或者,您可以使用- [UIImage drawAtPoint:] -[UIImage drawInRect:] 和其他类似的方法会自动处理比例尺。如果下拉至 CGImage ,则必须自己进行缩放。

Alternatively, you can use the -[UIImage drawAtPoint:], -[UIImage drawInRect:] and other similar methods that deal with the scale automatically. If you drop down to CGImage, you have to handle scale yourself.

这篇关于在这种情况下,是否必须将比例乘以视网膜显示的点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆