I need to save the photo of imageView to a gallery, when I click on the "Add" button on the navigation bar. I'm trying to save it, but I have a "Thread 1: signal SIGABRT" error. Maybe someone knows, where is the problem?
class ViewControllerFilters2: UIViewController, UINavigationControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
var filteredImage: UIImage?
var imagePicker: UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
if imageView.image == nil {
imageView.image = filteredImage!
}
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "✔", style: .plain, target: self, action: #selector(imagePickerController(_:didFinishPickingMediaWithInfo:)))
}
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
dismiss(animated: true, completion: nil)
}
}
@objc func image (_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
}