UIImage的size:point
CGImage的size:px
所以如果前者的size用在后者,需要乘以scale。
可以看下这个链接:
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?
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.