-1

Using the following code on my IBACTION to put my UIImageview > UIImage > PNG in photo

However, when I press my IBACTION, the image is not in my gallery

    UIGraphicsBeginImageContextWithOptions(self.theView.bounds.size, view.isOpaque, 0)
    theView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let theCapturedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    let data = theCapturedImage!.pngData()
    let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(NSUUID().uuidString+".dat")
    do {
        try data!.write(to: url!, options: [])
    } catch let e as NSError {
        print("Error! \(e)");
        return
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
chuckbass
  • 95
  • 7
  • this code writes it in temporary directory , what extension `.dat` ? check https://stackoverflow.com/questions/40854886/swift-take-a-photo-and-save-to-photo-library – Shehata Gamal Dec 16 '18 at 23:05

1 Answers1

0

Instead of writing the data to the temporary directory, try this inside the do block:

let image = UIImage(data: data)
UIImageWriteToSavedPhotosAlbum(data, nil, nil, nil) 

Remember to ask for the necessary permissions to save into the gallery, by setting the Privacy - Photo Library Usage Description key with an appropriate description in info.plist.

Renzo Tissoni
  • 642
  • 9
  • 21