0

Below I convert a screen capture into a NSData UIImagePNGRepresentation.

But how do I correct the below code and convert a screen capture into a UIImagePNGRepresentation but ensure it's using a UIImage format and not NSData?

// Take snapshot of screen
var imageSnapshot: UIImage!
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0)
self.view.drawViewHierarchyInRect(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height), afterScreenUpdates: false)
imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

// Prepare image to PNG format
let imageShare: NSData
imageShare = UIImagePNGRepresentation(imageSnapshot)!
user4806509
  • 2,925
  • 5
  • 37
  • 72
  • 1
    `UIImage` is not a format, it's a class that manages a reference to an image so it can be displayed inside a `UIImageView`. The difference between `UIImage` and `NSData` is mostly semantic, and you can easily convert from one to other (e.g. `UIImage(data: imageData)`). And in your code you're starting from a `UIImage` and generating an `NSData` instance from it. – shim Jul 17 '17 at 20:55
  • 1
    `imageSnapshot` is already a `UIImage`, so its not clear what you're asking for. – Craig Siemens Jul 17 '17 at 20:56
  • Thanks. On this other question I received the answer “Try to use UIImage instead NSData. You can convert UIImage to AnyObject and add to your array.” But I don’t understand how to do that. I'm rather confused about this. https://stackoverflow.com/a/45144670/4806509 – user4806509 Jul 17 '17 at 20:58
  • You already have a `UIImage` and you're converting it to `NSData` for some reason. The answerer on that other question is just saying to use the `UIImage`. You don't need to convert it to a PNG. – shim Jul 17 '17 at 21:00
  • The output of the image when I save the `imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()` appears to be a lossy jpg, so I'm converting to NSData PNG which gives a perfect image. But Is that still an UIImage or NSData? – user4806509 Jul 17 '17 at 21:06
  • 1
    not related to your issue but why not `view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: false)`. BTW you should use Data instead of NSData in Swift 3 – Leo Dabus Jul 17 '17 at 21:11
  • The quality of the image would not be magically improved by "converting" it from a JPG to a PNG (note there is no compression on a UIImage, that's only once it's saved to a file, which for example is handled by the activity you select in UIActivityViewController). If you want the image when downloaded after sharing etc to be a PNG instead of a JPG then I assume you could convert it like this, but I don't see the point. It's not going to make a difference to the quality, and it's typical for images like screenshots to be shared as JPG anyway. – shim Jul 17 '17 at 21:13
  • UIGraphicsGetImageFromCurrentImageContext() returns an UIImage and it doesn't have any compression. – Leo Dabus Jul 17 '17 at 21:13
  • Thanks, @Leo Dabus Nice tip. – user4806509 Jul 17 '17 at 21:13
  • @Shim, there's definitely a noticeable difference between the two outputs in quality when saving or sharing using the `UIActivityViewController`. Double tap to zoom non `UIImagePNGRepresentation` is fuzzy and includes artefacts. – user4806509 Jul 17 '17 at 21:16
  • Like I said, whatever happens after the image is shared or saved is handled by the activity. If you want to ensure there's no loss due to compression then I guess you can do what you did [here](https://stackoverflow.com/a/45147070/1032372). What is the purpose of this separate question if you already solved the issue? – shim Jul 17 '17 at 21:19

0 Answers0