-1

I'm trying to share an images using UIActivityViewController (on iMessage, Telegram, WhatsApp and other).

I'm doing:

let image = ... // an image
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)

But the image is cropped very badly. I've also tried to resize the image before the user share action, using UIActivityItemSource, but the result change depending the user device and the image

How can I achieve a perfect preview on all device and apps? Any advice or reference?

Here an example of the final result:

Sample share

The first image is the image shared as Sticker in iMessage and the second using UIActivityViewController

PGLongo
  • 439
  • 4
  • 13
  • What do you mean "the image is cropped very badly"? Do you mean that when the user actually retrieves the image in another app, it's the wrong image? If so, you need to show _that_ code, not the code by which you present the activity view controller. – matt Aug 22 '17 at 16:42
  • The code is what I've posted. You have focused on the resize but that was only an experiment that partially achieved the goal – PGLongo Aug 22 '17 at 18:23
  • What *exactly* is the issue? You've indicated that it isn't cropping nor compression, yet you really haven't detailed *the issue*. I share images using UIActivityController all the time, and I have no problems. How can any of us help you when we have no way to *duplicate* whatever the issue is? –  Aug 22 '17 at 18:47
  • "You have focused on the resize but that was only an experiment that partially achieved the goal" I haven't focused on anything. I'm saying that _you_ haven't focused on what the problem is. What is this "preview" to which you refer? What is being "cropped very badly", and where? – matt Aug 22 '17 at 19:02
  • I've added an example of what I obtain. Using `UIActivityViewController ` I obtain the second image, the preview is cropped, but clicking the image I can see the full resolution image – PGLongo Aug 22 '17 at 19:03
  • But that's not up to you. You don't control how Messages crops the display of your image! You are successfully passing the image to Messages; that's your lot. – matt Aug 22 '17 at 19:03
  • And that's could be fine. But there should be some reference on how iMessage crop the image. If I discover the the image should be, for example, 300x100 I could resize the image to avoid the crop and achieve the perfect preview – PGLongo Aug 22 '17 at 19:09
  • Well, there is no such reference. You have no idea what some other app will do with the display of your image, and it should be no concern of yours anyway. The other app is not your app. – matt Aug 22 '17 at 20:23
  • @matt I partially agree with you, but we are talking about iMessage, not just "another app", I was hoping to find something in the documentation that could be useful but unfortunately you are right and seem not possible, even if using 'UIActivityItemSource' and resizing the image I've partially achieved my goal – PGLongo Aug 22 '17 at 20:55
  • But the question was "How can I achieve a perfect preview on all device and apps", remember? I'm saying you can't, it's not up to you. – matt Aug 22 '17 at 22:00

1 Answers1

0

Try to compress image before use UIActivityViewController

var compressedImage: Data? = UIImageJPEGRepresentation(yourImage, 0.8)
            var docsPath: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as? String ?? ""
            var imagePath: String = URL(fileURLWithPath: docsPath).appendingPathComponent("image.jpg").absoluteString
            var imageUrl = URL.init(fileURLWithPath: imagePath)

            do {
                try compressedImage?.write(to: imageUrl, options: .atomic)
            } catch {
                print(error)
            }

            // save the file
            var activityViewController = UIActivityViewController(activityItems: ["Check this image", imageUrl], applicationActivities: nil)
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
  • Tried in this way and it doesn't work: `let data = UIImageJPEGRepresentation(self.image, 0.8)! let image = UIImage(data: data)! //let image = UIImage(data: data, scale: 1)! //let image = UIImage(data: data, scale: 3)! let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil) self.present(activityViewController, animated: true, completion: nil)` – PGLongo Aug 22 '17 at 18:32