-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
1

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.