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,
You should also consider doing the picker.dismiss
first and do the other logic with the image afterward. That way, you can prevent the user from tapping an image multiple times and invoking the delegate function several times.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard !picker.isBeingDismissed else {
return
}
picker.dismiss(animated: true) {
if let pickedImage = (info[UIImagePickerController.InfoKey(rawValue: UIImagePickerController.InfoKey.originalImage.rawValue)] as? UIImage) {
// do stuff with the picked image
print("Uesr picked an image \(pickedImage)")
}
}
}
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With