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