title: How to share Image in Swift
body: <p>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 </p>
  
  <pre><code>     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)
      }
  </code></pre>
  
  <p>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 <code>let tempImage = UIImage(named: "image.png")</code>
  and tap share button, and it works perfectly. </p>
  
  <p>So I figured out, did I have to save my image to local file first and then called it ? </p>
  
  <p>If yes how ? I tried use this code but failed. </p>
  
  <pre><code> // Create path.
   let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
          let filePath = "\(paths[0])/temp.png"
  
   // Save image.
   UIImagePNGRepresentation(image!)?.write(to: filePath)
  </code></pre>
  
  <blockquote>
    <p>the error is : cannot convert value of type 'String' to expected
    argument type 'URL' in this line <code>
    UIImagePNGRepresentation(tempImage!)?.write(to: filePath)</code></p>
  </blockquote>
  
  <p>Or maybe I dont need to save image? how ?</p>
  
  <p>Thank you</p>