1

Following these steps I'm getting an unexpected result:

1) Write UIImage to PNG

2) Read PNG into UIImage

Result: the sizes of the UIImages (before and after PNG) are different. The UIImage created by reading from the PNG has twice the resolution as the original UIImage used to create the PNG.

Swift pseudocode follows:

var initialImage : UIImage;
// Obtain UIImage from UIImagePickerController, Images.xcassets, ...
// As an example, consider a UIImage with size = (420.0, 280.0).

println("Image size = \(initialImage.size)") // Indicates "Image size = (420.0, 280.0)"

// Write image to file
UIImagePNGRepresentation(myImage).writeToFile(imagePath, atomically: true)

// Retrieve image from file
let newImage = UIImage(contentsOfFile: imagePath)!
println("Image size = \(initialImage.size)") // Indicates "Image size = (840.0, 560.0)"!

All is well otherwise. Displaying the newly read image works great.

I'm running this connected to an iPhone 6. Any thoughts would be greatly appreciated.

gary

SwiftAero
  • 69
  • 4

1 Answers1

0

It's converting it for the retina display. Each point is represented by 2 pixels on the screen so the saved image is double the resolution. This is a good article to reference: http://www.paintcodeapp.com/news/iphone-6-screens-demystified

Kex
  • 8,023
  • 9
  • 56
  • 129
  • Thank you for the response. I figured it had something to do with the Retina screen, though I'm still not understanding the complete mystery. If I write the last UIImage above to a different file, then read it in, it remains at the (840.0, 560.0) rather than doubles the resolution. Can you explain that? – SwiftAero Mar 03 '15 at 02:27
  • It could be that because the initial image is read from the imagePicker it does the work to store it for the retina resolution and doubles it up before saving it. In the second case you are reading first from a file, the dimensions are already set solid so it won't try and change them. – Kex Mar 03 '15 at 13:22
  • It's 2D, so it's 4 pixels and 4 times the resolution. – Nikita Volkov May 20 '15 at 11:47