0

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

Table of outcomes using Method 1 and Method 2.


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.

enter image description here

"Unable to load content" error message.

"Unable to load content" error message.

user4806509
  • 2,925
  • 5
  • 37
  • 72
  • Given that you have *three* answers to this question you asked one day ago (https://stackoverflow.com/questions/45144192/sharing-image-and-text-to-facebook-messenger-with-uiactivityviewcontroller-faili)... why is this question any different? Since you haven't accepted any of those answers **and** didn't delete that question, could you - specifically - do something with your questions (four in a day, the first looking to me like the issue was well stated but not *really* edited by you, and **none** of the other three related questions have accepted answers) - combine this into one question? –  Jul 18 '17 at 20:18
  • Could you try sharing as a file? https://stackoverflow.com/a/37753905/3708242 – wottle Jul 18 '17 at 20:30
  • @wottle I've adapted the suggested ideas, but how do I turn the snapshot into a PNG file in the first place? `var pngFile = URL(fileURLWithPath: "myfile.png")` `var items: [Any] = [pngFile]` `let UIActivityViewController(activityItems: items, applicationActivities: nil)` `presentViewController(activity, animated: true, completion: nil)` – user4806509 Jul 18 '17 at 23:35
  • @dfd none of the answers give the solution to the question. This question runs though two methods and gives the definitive outcome to try an hone a response that answers the question. – user4806509 Jul 18 '17 at 23:53
  • The issue I have is with what this site is about. You've asked a few questions - which we aren't here for, we're here to help *everyone* - and you've asked a *original* question that pretty much is what *you* want to figure out. First? I'm glad we've helped some. But reading the first question I had to flag you because it seems you don't really want our help unless it fits what *you* think should work. (Oddly enough, my moderator flag is still pending, yet NOW you've accepted four answers.) Let me continue this. –  Jul 19 '17 at 01:48
  • Look. I have no issue with *you*. Nor your original question. But why ask question after question? Particularly when the first on is what not *just* mattered to you but pretty much everyone else? This particular question is but a subset of that one. Sure, you have a few *specific* issues you are working to fix. But could you limit yourself to a **single and specific** one over 24 hours? I really thought that, while multiple issues, you OP I linked to was a very good question. All I thought in my comment was that you needed to edit **that** question instead of asking yet another question. –  Jul 19 '17 at 01:55
  • @dfd no issue with you either, if you can't help, that's fine, but please don't comment in that case, it wastes all our time. I've had different thinking since the original question, coming at it from different angles. I've discovered new outcomes. These don't fit the narrative of that first question. A lot of the answers missed the essence of the original question in the first place. I haven't accepted any answers, I haven't green checked any because there is not clear answer to this issue. Something odd occurs only when `UIActivityViewController`, PNG data, and Facebook Messenger collide. – user4806509 Jul 19 '17 at 02:15
  • Sorry, you may be abusing the site. And since I flagged this question for moderator intervention, we *both* will know. Look, this site is for *everyone* - not me, and not you. It's meant for **good** Q&A - which I thought your first question, while multi-facteted, was quite good. (the site clearly state what MVCE is). While all of us care about **your** issue, we care about helping *all*! You've asked several questions in 24 hours. All are related. And no comments or answers seem to help you. Either "give it a rest" or "combine" everything to be of help to everyone. Please? :-) –  Jul 19 '17 at 02:26
  • @dfd, it's not your place to instruct other users how to form questions and I'm not abusing the site at all. You are abusing the comments area however. I understand this is a site for everyone. I don't particularly care if a moderator kills this question or not. This is a great question. It has a great analysis of the issues involved. If the question is killed, it would be rather astonishing considering there is a major problem for someone to solve here. Sharing with `UIActivityViewController` a `PNG` image to Messenger fails. An answer to this question helps everyone in the future. – user4806509 Jul 19 '17 at 02:44
  • Got it. I see you asked yet another related question. Me and you are done swapping comments. Your understanding of *good* questions is **obviously** different from mine. Peace. I tried to help. –  Jul 19 '17 at 02:49
  • Peace out, @dfd – user4806509 Jul 19 '17 at 02:54

0 Answers0