简体   繁体   English

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

[英]saving an image to photos library using Swift 2.0

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. 当我拍照并按下使用照片时,我不会将我拍​​摄的照片保存到iPad上的相册中。 I have looked at other ways but me being a beginner to iOS development, I'm unsure of how to fix this problem. 我已经看过其他方式,但我是iOS开发的初学者,我不确定如何解决这个问题。 I sometimes get a sigabrt error too. 我有时也会得到一个sigabrt错误。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM