Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePNGRepresentation() and scale (iPhone 4 screen)

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?

like image 689
Amy Worrall Avatar asked Jun 29 '10 17:06

Amy Worrall


1 Answers

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.

like image 186
Jason Foreman Avatar answered Oct 06 '22 02:10

Jason Foreman