views:

168

answers:

1

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  A: 

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 manually. If you drop down to CGImage, you have to handle scale yourself.

rpetrich