如何将UIImageView内容保存到图库? (迅速)

时间:2018-11-25 11:52:45

标签: ios swift uiimageview

几天前才开始尝试学习。我的UIImageView中有一张图片,如何将其保存到图库中?我创建了一个带有IBAction的按钮

.pngData()有关系吗?

谢谢

1 个答案:

答案 0 :(得分:1)

@IBAction func nameOfFunctionToSave(_ sender: AnyObject) {
  UIImageWriteToSavedPhotosAlbum(imageTake.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}

extension photoPickerController :  UIImagePickerControllerDelegate  {
    func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
         if let error = error {
          // in case we get an error
           let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
           alert.addAction(UIAlertAction(title: "OK", style: .default))
           present(alert, animated: true)
         } else {
          // in case of success
           let alert = UIAlertController(title: "Saved!", message: "Image saved successfully in your library", preferredStyle: .alert)
           alert.addAction(UIAlertAction(title: "OK", style: .default))
           present(alert, animated: true)
         }
}