简体   繁体   中英

How to save image from imageView to Photo Library?

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

}

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:

在此处输入图像描述

Hope it helps.. :)

First, we have to do the setup for Permissions inside Project's.plist file

for swift4

import UIKit

class photoPickerController: UIViewController,UINavigationControllerDelegate{

@IBOutlet weak var imageTake: UIImageView!

var imagePicker: UIImagePickerController!
override func viewDidLoad() {
    super.viewDidLoad()
}


@IBAction func takePhoto(_ sender: UIButton) {

    imagePicker =  UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    present(imagePicker, animated: true, completion: nil)
}


@IBAction func saveToLibrary(_ 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 {
        // we got back an error!
        let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))
        present(alert, animated: true)
    } else {
        let alert = UIAlertController(title: "Saved!", message: "Image saved successfully", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))
        present(alert, animated: true)
    }
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    imagePicker.dismiss(animated: true, completion: nil)
    imageTake.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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