0

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

}

Pete Ythong
  • 305
  • 5
  • 13
  • 1
    change `dismiss(animated: true, completion: nil)` to `picker.dismiss(animated: true, completion: nil)` – Mumtaz Hussain Oct 01 '19 at 09:16
  • I change, but the same problem. – kosya_kosia Oct 01 '19 at 09:20
  • Just check the link. Already answered. https://stackoverflow.com/a/40858152/4747927 – yankit Patel Oct 01 '19 at 10:10
  • do you want to save the image from image gallery itself? where is the picker controller initialised and called from? – Van Oct 01 '19 at 12:37
  • can you let us know error printed on console, can help to go to direction toward solution? – Van Oct 01 '19 at 12:50
  • When I compile the app, it doesn't have an error. But when I click on Bar Button Item, which should to save my photo, the app is broken and I have an error "Thread 19: signal SIGABRT" – kosya_kosia Oct 01 '19 at 15:36
  • 2019-10-01 18:53:48.876331+0300 PhotoEditor[5994:1802712] [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data. – kosya_kosia Oct 01 '19 at 15:54

1 Answers1

0

The method getting called on tick/add button seems wrong, the target should be uibarbutton item for calling method:

     navigationItem.rightBarButtonItem = UIBarButtonItem(title: "✔", style: .plain, target: self, action: #selector(imagePickerController(_:didFinishPickingMediaWithInfo:)))

Change selector for saving the image which will have code for saving the image to albums like:

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "✔", style: .plain, target: self, action: #selector(saveImage))

and the save image method may be like:

    @objc func saveImage() {
    if let pickedImage = imageView.image {
        UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
    }
   }

Also , in imagePickerController didFinish method assign the image to image view so that new image would be saved on click of add.

EDIT

As per crash log, add usage in formation in your plist for NSPhotoLibraryAddUsageDescription as:

enter image description here

Hope it helps.. :)

Van
  • 1,225
  • 10
  • 18
  • Sorry, I don't understand, what you mean in the last sentence. I change all you said. But what I need to do with imagePickerController? – kosya_kosia Oct 01 '19 at 15:40
  • Okey, I add it. But when I click on the navigation button "Add", I have an alert my photo save. But when I open the gallery, there aren't my photo – kosya_kosia Oct 04 '19 at 07:04
  • @kosya_kosia does your image has image at time of saving the image to gallery? – Van Oct 10 '19 at 06:36