5

This is a question for an expert with specialist knowledge.

It is possible to successfully share high quality lossless PNG images with UIActivityViewController to many sharing platforms with the exception of Facebook Messenger which fails every time. The error provided is “Couldn’t Load Content”.

Both UIImagePNGRepresentation NSData and AnyObject fail in Facebook Messenger, UIImage however shares successfully BUT the outputted image appears to be a JPG and is low quality and lossy.

Question:

What’s going on here and how can it be corrected -- how can I successfully share high quality lossless PNG images with UIActivityViewController to Facebook Messenger?

Is this a problem or limitation of Facebook Messenger, Xcode, or UIActivityViewController?

Are there alternatives to NSData and AnyObject (and UIImage) that will work?

Code:

var myImage: UIImage! 
var myImagePNG: NSData! 
//var myImagePNG: AnyObject!

func sharePNG() { …
    myImagePNG = UIImagePNGRepresentation(myImage)!
    let activity = UIActivityViewController(activityItems: [myImagePNG], applicationActivities: nil)
    self.presentViewController(activity, animated: true, completion: nil)
}

Image:

enter image description here

user4806509
  • 2,925
  • 5
  • 37
  • 72
  • What do you want to do?You shoud use `myImage` instead of `myImagePNG`. – Codus Jul 19 '17 at 02:21
  • @Codus, thanks but I think you need to reread the question. `myImage` gives lossy low quality image output. `myImagePNG` gives a lossless high quality image output when sharing with all apps except for Messenger which fails. I'm after lossless high quality image output when sharing to Messenger, that's what I want to do. I want to use `myImagePNG` like I successfully do when sharing to all other apps. – user4806509 Jul 19 '17 at 02:25
  • I have the same issue. Only occurs when sharing to Facebook Messenger. Did you find a solution already? – codeDude Sep 06 '17 at 07:58
  • 1
    @codeDude sorry for the delay in reply. Unfortunately no, a solution was not found. I resigned to the fact Facebook Messenger currently does not cooperate with iOS sharing. The lossy image remains when sharing. If that's what Facebook Messenger wants, that's what Facebook Messenger gets, lossy images. :-) – user4806509 Oct 10 '17 at 02:33
  • Further @codeDude , on a related question, this might point to some sort of solution: https://stackoverflow.com/questions/43380570/share-image-with-hashtag-via-uiactivityviewcontroller-twitter-facebook-instag – user4806509 Oct 10 '17 at 02:52
  • 1
    @user4806509 Thanks for shedding some light on the matter. Hehe, yea it is their loss....lossy images – codeDude Oct 13 '17 at 15:15

2 Answers2

1

The best way to get more control over what gets shared from UIActivityController is not just to encode it to data, but then to write that data to a temporary URL, and then pass the URL to UIActivityController. That way you can control the title of the file being shared, which is important if they choose to save the file or AirDrop it somewhere. If you just share the data you'll end up with some really random filename. This works for sending PNGs via Facebook Messenger:

    enum ImageProcessingError: Error {
        case couldNotCreatePNGData
    }
    
    private func exportImageAsPNG(_ image: UIImage, filename: String) throws {
        guard let pngData = image.pngData() else { throw ImageProcessingError.couldNotCreatePNGData }
        let temporaryURL = FileManager.default.temporaryDirectory
            .appendingPathComponent(filename)
            .appendingPathExtension("png")
        try pngData.write(to: temporaryURL, options: [])
        // present UIActivityViewController with the temporaryURL
    }
OliverD
  • 1,074
  • 13
  • 19
0

to keep the quality of the images try using UIGraphicsBeginImageContextWithOptions()

Maruta
  • 1,063
  • 11
  • 24
  • 1
    Thanks. I already use `UIGraphicsBeginImageContextWithOptions()` and then `drawViewHierarchyInRect` to capture. The capture is perfect quality. It's actually at the point of sharing when things go odd with Messenger. – user4806509 Aug 05 '17 at 09:51