iosswiftswift3uiimagepickercontroller

prevent picking the same photo twice in UIImagePickerController


How do i prevent users from picking the same image twice in UIImagePickerContoroller to avoid duplication?

I tried doing it with the URLReference but its not working so I'm guessing its not the way.

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

thanks,


Solution

  • Seems like you haven't appended the url to the photosURL? try this out:

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