简体   繁体   中英

How to save photo only when taken with camera?

I have a view controller with UIImagePickerControllerDelegate and UINavigationControllerDelegate to pick photo.

But when I pick photo and then it auto save a same photo in my photo album.

I guess I write this function in my code

UIImageWriteToSavedPhotosAlbum(photoImage!, nil, nil, nil)

so, I check sourcetype equal to camera then save the photo and pick photo in album not save the same photo again .
But it doesn't work. what's happen?
It also auto save the same photo.
I don't want to save the same photo?

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let uuid = NSUUID().uuidString
    let imageName:String = uuid + ".jpg"
    let documentsPath = NSHomeDirectory().appending("/Documents/\(image)/")
    let imagePath = documentsPath.appending(imageName)
    let imageUrl = URL(fileURLWithPath: imagePath)
    print("imageUrl is here:\(imageUrl)")

    photoImage = info[UIImagePickerControllerOriginalImage] as? UIImage

    if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {

        photoImage = info[UIImagePickerControllerOriginalImage] as? UIImage
        // this doesn't work!!!!  
        //UIImageWriteToSavedPhotosAlbum(photoImage!, nil, nil, nil)
    }

    //save image to local Documents
    let imageData:Data = UIImageJPEGRepresentation(photoImage!, 0.05)!
    do {
        try imageData.write(to: imageUrl,options: .atomic)
    } catch let error {
        print(error)
    }

    showLoading()

    hideLoading {

        //I have func upload image to server
        self.tableView.reloadData()
        self.dismiss(animated: true, completion: nil)
    }
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

    self.dismiss(animated: true, completion: nil)
}

You are not checking the image picker controller's source type, you are checking if the camera is available in general.

Change this:

if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {

to:

if picker.sourceType == .camera {

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