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

4
我遇到了上面用红色标出的问题。当我拍照并按下使用照片时,它不会让我将所拍摄的照片保存到iPad的相册中。我已经尝试了其他方法,但作为iOS开发的初学者,我不确定如何解决这个问题。有时我也会收到sigabrt错误。

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


你实现了回调方法吗?func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) { ... } - TotoroTotoro
你遇到了具体的错误是什么?它在你的图片中被裁剪掉了。 - mszaro
2个回答

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接