prevent picking the same photo twice in UIImagePickerController
Asked Answered
M

4

9

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,

Mcginley answered 24/5, 2017 at 9:10 Comment(3)
Add your done code for imagePicker.Rebroadcast
the info[UIImagePickerControllerReferenceURL] should be unique for each images so you can keep that to check if it is already pickedAfricah
tried it already, when I'm checking if photosURL.contains(url) it returns falseMcginley
S
0

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)
}
Soyuz answered 24/5, 2017 at 10:0 Comment(0)
M
14

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)")
        }
    }
}
Mcavoy answered 18/11, 2017 at 15:27 Comment(4)
If the user spam the image with multiple-fast taps, it will call the delegate function multiple times anyway.Acceptable
@Acceptable I haven't tested this. But if that's a concern, maybe we can add one more check on beingDismissed.Mcavoy
@YuchenZhong Can you please add that answer about where we can add that check on beingDismissed ?Keg
@MiteshDobareeya I updated the answer above to include the check of isBeingDismissed and also updated the code to the latest version of Swift (i.e. Swift 5.2).Mcavoy
G
4

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)
}
Godliman answered 15/1, 2019 at 20:33 Comment(2)
What is convertFromUIImagePickerControllerInfoKeyDictionary(info) please clarify.Harday
Faced with the same problem and setting delegate to nil fixed it. Thanks a lot, mate!Pasta
B
1

preselectedAssetIdentifiers

If you have chosen PHPicker while creating a gallery picker in an application you created with SwiftUI, and you want to prevent this picker from selecting the selected photos over and over again, you can apply the following method.

The feature that will prevent the user from selecting the same photo twice is "preselectedAssetIdentifiers"

import Foundation
import PhotosUI
import SwiftUI

struct GalleryPicker: UIViewControllerRepresentable {
    func makeCoordinator() -> Coordinator {
        return GalleryPicker.Coordinator(parent: self)
    }

    @Binding var images: [UIImage]
    @Binding var picker: Bool
    @Binding var preSelected: [String]

    func makeUIViewController(context: Context) -> PHPickerViewController {
        var configuration = PHPickerConfiguration(photoLibrary: .shared())
        configuration.preselectedAssetIdentifiers = preSelected
        configuration.filter = .images
        configuration.selection = .ordered
        configuration.selectionLimit = 0
        let picker = PHPickerViewController(configuration: configuration)
        picker.delegate = context.coordinator.self
        return picker
    }

    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
    }

    class Coordinator: NSObject, PHPickerViewControllerDelegate {
        var parent: GalleryPicker
        init(parent: GalleryPicker) {
            self.parent = parent
        }

        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
            parent.picker.toggle()
            
            for result in results  {
                let provider = result.itemProvider
                let assetIdentifier = result.assetIdentifier ?? ""
                print("asset --> \(assetIdentifier)")
                if provider.canLoadObject(ofClass: UIImage.self) {
                    provider.loadObject(ofClass: UIImage.self) { image, _ in
                        DispatchQueue.main.async {
                            self.parent.preSelected.append(assetIdentifier)
                            self.parent.images.append(image as! UIImage)
                        }
                    }
                }
            }
        }
    }
}

screenshot

Bergwall answered 22/11, 2022 at 18:22 Comment(0)
S
0

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)
}
Soyuz answered 24/5, 2017 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.