使用Swift 2.0将图像保存到照片库

时间:2016-03-03 01:05:28

标签: ios swift uiimagepickercontroller imagelibrary

我在上面用红色突出显示了一个问题。当我拍照并按下使用照片时,我不会将我拍​​摄的照片保存到iPad上的相册中。我已经看过其他方式,但我是iOS开发的初学者,我不确定如何解决这个问题。我有时也会得到一个sigabrt错误。

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

2 个答案:

答案 0 :(得分:8)

您是否提供了完成选择器?

...
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)
    }
}

答案 1 :(得分:0)

你可以使用它;

@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)
}