views:

267

answers:

1

I've got some image generating code that uses UIGraphicsBeginImageContext(), UIGraphicsGetImageFromCurrentImageContext() and UIImagePNGRepresentation() to do some drawing, then save it to disk as a PNG for later use.

Does UIImagePNGRepresentation() take into account scale? As in, if I have an image that is 20 points wide, will the resultant PNG be 20 pixels or 40 pixels?

Also, when I display these images, I use [UIImage imageWithContentsOfFile:] and [image drawInRect:]. Is there a way to hint to those methods to use higher resolution drawing?

+1  A: 

Per the iPhone Application Programming Guide you should use UIGraphicsBeginImageContextWithOptions with a scale of 2.0 to create a properly scaled context for the iPhone 4.

As for the second part of your question, I believe you should save the resulting png with the @2x suffix on the base name (e.g., [email protected]). Then when you load it back in using UIImage, its size and scale will be correctly set. Otherwise your image will be loaded at scale 1.0 and be twice as large (in points) as you expect it to be. This section of the same document goes into a bit of detail regarding high-resolution images for devices with high-resolution displays.

Jason Foreman
If I created the context with the 2.0 scale (say it's 20 points wide), then when I call UIImagePNGRepresentation() will the resulting PNG be 40 pixels wide?
Amorya
According to my understanding of the docs, yes. Though I admit I haven't tried it myself.So you create a 20x20 pt context at 2.0 scale factor. You get an image of 40x40 pixels. You save that as [email protected]. Then when you load it with UIImage, it will again be 20x20 pt with scale of 2.0. If instead you save it as just image.png and load it, you'll get an image 40x40 pt with scale 1.0.
Jason Foreman
Thanks for your help. I got it working :)
Amorya