Prevent simultaneous sampling of the same photo in UIImagePickerController

How can I prevent users from selecting the same image twice in the UIImagePickerContoroller to avoid duplication?

I tried doing this with URLReference but didn't work, so I'm guessing it isn't.

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

    if let url = info[UIImagePickerControllerReferenceURL] as? NSURL{
        if photosURL.contains(url){
             Utilities.showMessage(message: "photo Uploaded already", sender: self, title: ErrorTitle.FRIENDS, onDismissAction: nil)
        } else {
            if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
                photos.append(pickedImage)
            }
        }
    }
    dismiss(animated: true, completion: nil)
}

      

thank,

+3


source to share


3 answers


Looks like you haven't added url to photosURL? try it:



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

if let url = info[UIImagePickerControllerReferenceURL] as? NSURL{
    if photosURL.contains(url){
         Utilities.showMessage(message: "photo Uploaded already", sender: self, title: ErrorTitle.FRIENDS, onDismissAction: nil)
    } else {
        photosURL.append(url)
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            photos.append(pickedImage)
        }
    }
}
dismiss(animated: true, completion: nil)
}

      

0


source


You should also consider first picker.dismiss

and then do other logic with the image. This way you can prevent the user from clicking the image multiple times and call the delegate function multiple times.



func imagePickerController(_ picker: UIImagePickerController,
                                  didFinishPickingMediaWithInfo info: [String : Any]) {
    picker.dismiss(animated: true) {
        if let pickedImage = (info[UIImagePickerControllerOriginalImage] as? UIImage) {
            // do stuff with the picked image
        }
    }
}

      

+3


source


Swift 4

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

    let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)

    if let capturedImage = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage {
        // do stuff
    }

    picker.delegate = nil
    picker.dismiss(animated: true, completion: nil)
}

      

0


source







All Articles