问题
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.
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]);
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]));
Output in console: image height = 31.000000 (CGImage = 62)
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?
回答1:
Yes.
CGImageGetWidth([image CGImage]) == image.size.width * image.scale
CGImageGetHeight([image CGImage]) == image.size.height * image.scale
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.
来源:https://stackoverflow.com/questions/3745126/must-i-multiply-the-scale-with-the-point-values-for-retina-display-in-this-case