Dowemo
0 0 0 0


Question:

I am Swift beginner and just completed my Photo camera function in my app. The problem which I have now is that when I take a photo from the camera, it is not saved into the photo library on my iPhone. Everything works perfectly I am able to take pictures, but when I check in Photos, it seems that they are not saved. I had checked for similar questions, but I haven't found a proper answer, I see that they are people who add buttons to access the photo library directly but I do not need such a button. The only function, I require is taking a picture and when the user clicks select photo to save it in the Photos.

So far I have used this:

class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate{
 let imagePicker: UIImagePickerController! = UIImagePickerController()
   override func viewDidLoad() {
    super.viewDidLoad()
    imagePicker.delegate = self
    let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes))
    upSwipe.direction = .up
    view.addGestureRecognizer(upSwipe)
}

and the function:

func handleSwipes(sender:UISwipeGestureRecognizer) {
        if (sender.direction == .up){
            if ( UIImagePickerController.isSourceTypeAvailable(.camera)){
                if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
                    imagePicker.allowsEditing = false
                    imagePicker.sourceType = .camera
                    imagePicker.cameraCaptureMode = .photo
                    present(imagePicker,animated: true, completion: {})
       }
            }

Here is what I get on my iPhone: I only want when the user chooses to use the photo, the photo itself to be saved in the gallery, as simple as that.enter image description here


Best Answer:


Before anything you need to have the "Privacy - Photo Library Additions Usage Description" to your info.plist otherwise your app will crash

Use this:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
            UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
            dismiss(animated: true, completion: nil)
        }
    }

And then, add the following function (The same function that saltTigerK wrote)

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

Source: https://www.hackingwithswift.com/read/13/5/saving-to-the-ios-photo-library




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs