saving an image to photos library using Swift 2.0

5.3k Views Asked by At

I'm getting a problem highlighted in red above. When I take a picture and press use photo, it won't let me save the photo I took to the photo album on my iPad. I have looked at other ways but me being a beginner to iOS development, I'm unsure of how to fix this problem. I sometimes get a sigabrt error too.

https://i.stack.imgur.com/Gb7o3.png

2

There are 2 best solutions below

0
On

You can use this;

@IBAction func downloadButton(_ sender: Any) {
    let imageRepresentation = UIImagePNGRepresentation(photoView.image!)
    let imageData = UIImage(data: imageRepresentation!)
    UIImageWriteToSavedPhotosAlbum(imageData!, nil, nil, nil)

    let alert = UIAlertController(title: "Completed", message: "Image has been saved!", preferredStyle: .alert)
    let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
    alert.addAction(action)
    self.present(alert, animated: true, completion: nil)
}
0
On

Have you provided the completion Selector?

...
UIImageWriteToSavedPhotosAlbum(imageView.image!, self, "image:didFinishSavingWithError:contextInfo:", nil) 
...



func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
    if error == nil {
        let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    } else {
        let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    }
}