【问题标题】:saving an image to photos library using Swift 2.0使用 Swift 2.0 将图像保存到照片库
【发布时间】:2016-06-16 03:12:48
【问题描述】:

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

【问题讨论】:

  • 你实现了回调方法吗? func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) { ... }
  • 您遇到的具体错误是什么?它已从您的图像中删除。

标签: ios swift uiimagepickercontroller imagelibrary


【解决方案1】:

你提供了补全选择器吗?

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

【讨论】:

    【解决方案2】:

    你可以用这个;

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

    【讨论】:

      猜你喜欢
      • 2015-10-20
      • 2018-10-03
      • 2021-12-07
      • 2020-06-07
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多