0

I tried to share a image that I generate by my own. However when I tried to share the image it doesn't works well. here is the code to share

     func shareQR(){
        //mainImage is the view that hold the image I want to share
        let tempImage = view.mainImage.image

        let imageToShare = [ tempImage ]
        let activityViewController = UIActivityViewController(activityItems: imageToShare as! [UIImage], applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view 

        activityViewController.excludedActivityTypes = [ UIActivityType.airDrop ]

        self.present(activityViewController, animated: true, completion: nil)
    }

So I tried to debug and the result is my tempImage value is the same with the one I want to share. And then I tried to hardcode the image and become let tempImage = UIImage(named: "image.png") and tap share button, and it works perfectly.

So I figured out, did I have to save my image to local file first and then called it ?

If yes how ? I tried use this code but failed.

 // Create path.
 let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let filePath = "\(paths[0])/temp.png"

 // Save image.
 UIImagePNGRepresentation(image!)?.write(to: filePath)

the error is : cannot convert value of type 'String' to expected argument type 'URL' in this line UIImagePNGRepresentation(tempImage!)?.write(to: filePath)

Or maybe I dont need to save image? how ?

Thank you

StevenTan
  • 275
  • 1
  • 5
  • 20

1 Answers1

0

It seems like your variable image is a String variable instead of a UIImage (hence the error that you have posted). To confirm this theory, add the following line in your code

print(tempImage)

after

let tempImage = view.mainImageimage

If it prints the image name, then your variable is a String variable. If it prints something along the lines of UIImage, then it's an actual image and we can further investigate the issue. Let me know what you get in result

Edit: To store a file locally, use the following code

if let data = UIImagePNGRepresentation(tempImage) {
    let filename = getDocumentsDirectory().appendingPathComponent("copy.png")
    try? data.write(to: filename)
}

func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}
Malik
  • 3,763
  • 1
  • 22
  • 35
  • this is the result `Optional(, {153, 153})` – StevenTan May 29 '17 at 04:16
  • so it seems that the `tempImage` is in fact storing a `UIImage`. My next question would be that what is the difference between `view.mainImage.image` and `mainView.barcodeImage.image`. Since you are not using the `tempImage` in the code snippet that you have shared, you might want to replace `mainView.barcodeImage.image` with `tempImage`. Alternatively, you can try to print out the value of `imageToShare` to make sure that it is in fact a `UIImage` as well – Malik May 29 '17 at 04:19
  • ah yes sorry let me fix my code a little bit. That's a wrong one I tried to manipulate before but failed. – StevenTan May 29 '17 at 04:20
  • One thing I can say for sure is that the `image` variable in `UIImagePNGRepresentation(image!)?.write(to: filePath)` is a `String` variable judging by the error. – Malik May 29 '17 at 04:21
  • the code is fixed. just a minor typo there. Did I have to save my image to local first to share image ? or I don't need to do that ? Because I've tried to share image using local & without using local image. Using local image can be shared but without can't – StevenTan May 29 '17 at 04:25
  • You need to use a URL. Just get the documentDirectory URL and append pathComponent (fileName) to it. https://stackoverflow.com/a/33812674/2303865 – Leo Dabus May 29 '17 at 04:27
  • As per my understanding, the line `let activityViewController = UIActivityViewController(activityItems: imageToShare as! [UIImage], applicationActivities: nil)` expects an array of `fileURL`s instead of an array of `UIImages`. You might have to store the images locally and then use the `fileURL`s of said images – Malik May 29 '17 at 04:31
  • then how should i write in code ? i'm a little bit confused here – StevenTan May 29 '17 at 04:40
  • Note that saving the image as PNG will discard its orientation info – Leo Dabus May 29 '17 at 04:57
  • I use the code you given, now the Share activity is become nothing. Before there's option to shared, save image and etc. now All disappear – StevenTan May 29 '17 at 05:01
  • and I tried to debug the code you give to me after this part it just skip over so the code doesn't go inside. `if let data = UIImagePNGRepresentation(tempImage)` – StevenTan May 29 '17 at 05:07
  • Let's continue the discussion in chat http://chat.stackoverflow.com/rooms/145335/how-to-share-image-in-swift – Malik May 29 '17 at 05:17