I have two methods that can take a screen snapshot and share the captured image to other apps presenting the UIActivityViewController.
Method 1 simply takes a screen snapshot and shares the captured image as UIImage.
Method 2 is very similar but includes an additional step. Method 2 takes a screen snapshot but instead uses a UIImagePNGRepresentation before sharing the captured image as NSData.
Each method used produces a difference in quality of the image shared. Method 1 produces image quality that is either lossless (i.e. Message and Mail) or lossy (i.e. Notes, Photos, Messenger) while Method 2 produces image quality that is lossless for all apps (with the exception of Messenger which produces an error Unable to load content and is unable to share).
Using Method 2 is the obvious choice for keeping shared images perfect but...
Questions
Why is Messenger producing an error Unable to load content?
What can be done to change the code below or what object can be used to ensure all images share, 1) in a lossless format, and, 2) with no error?
Table
Code
import UIKit
class ViewController: UIViewController {
var imageSnapshot: UIImage!
var imageSnapshotPNG: NSData!
func screenSnapshot() {
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()
}
@IBAction func shareImage(sender: UIButton) {
screenSnapshot()
let activity = UIActivityViewController(activityItems: [imageSnapshot], applicationActivities: nil)
self.presentViewController(activity, animated: true, completion: nil)
}
@IBAction func shareImagePNG(sender: UIButton) {
screenSnapshot()
imageSnapshotPNG = UIImagePNGRepresentation(imageSnapshot)!
let activity = UIActivityViewController(activityItems: [imageSnapshotPNG], applicationActivities: nil)
self.presentViewController(activity, animated: true, completion: nil)
}
}
Images
Presenting UIActivityViewController in iOS.
"Unable to load content" error message.


