1

I've tried to save UIImage on UIViewcontroller named secondViewController, But didn't work. Code snippet for saving image is attached.

@IBAction func takePhoto(_ sender: Any) {
    cameraSession.stopRunning()
    UIImageWriteToSavedPhotosAlbum(self.cameraImageView.image!, nil, nil, nil)
}

On SecondViewController, UIImage has been saved to cameraImageView.image,

But when check camera roll, nothing saved.

Does anyone know why?

Vasucd
  • 357
  • 2
  • 10
HEOJIN
  • 41
  • 4

2 Answers2

1

hey I added full code to capture image using UIImagePickerController and also added code to import a photo from photo gallery, hope it will helpful for you

import UIImagePickerControllerDelegateand create a variable to assign UIImagePickerController var imagePicker = UIImagePickerController() and set imagePicker.delegate = self.

Create a action sheet to display options for 'Camera' and 'Photo library'.

On your button click action:

@IBAction func buttonOnClick(_ sender: UIButton)
{
    self.btnEdit.setTitleColor(UIColor.white, for: .normal)
    self.btnEdit.isUserInteractionEnabled = true

    let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.openCamera()
    }))

    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
        self.openGallary()
    }))

    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

    /*If you want work actionsheet on ipad
     then you have to use popoverPresentationController to present the actionsheet,
     otherwise app will crash on iPad */
    switch UIDevice.current.userInterfaceIdiom {
    case .pad:
        alert.popoverPresentationController?.sourceView = sender
        alert.popoverPresentationController?.sourceRect = sender.bounds
        alert.popoverPresentationController?.permittedArrowDirections = .up
    default:
        break
    }

    self.present(alert, animated: true, completion: nil)
}

func openCamera() {
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
    {
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

func openGallary() {
    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    imagePicker.allowsEditing = true
    self.present(imagePicker, animated: true, completion: nil)
}
Mahmut Acar
  • 713
  • 2
  • 7
  • 26
Shivam Parmar
  • 1,520
  • 11
  • 27
1

Add Permission in your info.plist

"Privacy - Photo Library Additions Usage Description"

After permission granted try to save image.

@IBAction func takePhoto(_ sender: Any) {
    cameraSession.stopRunning()
    UIImageWriteToSavedPhotosAlbum(self.cameraImageView.image!, nil, nil, nil)
}
Vasucd
  • 357
  • 2
  • 10