ios 防止在UIImagePickerController中拾取同一张照片两次

4xrmg8kj  于 2022-11-26  发布在  iOS
关注(0)|答案(4)|浏览(916)

如何防止用户在UIImagePickerContoroller中两次拾取同一图像以避免重复?
我试着用URLReference做,但是它不起作用,所以我猜它不是这样的。

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

谢谢你,

zhte4eai

zhte4eai1#

你还应该考虑先执行picker.dismiss,然后再对图像执行其他逻辑,这样就可以防止用户多次点击图像和多次调用delegate函数。

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)")
        }
    }
}
1szpjjfi

1szpjjfi2#

雨燕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)
}
8zzbczxx

8zzbczxx3#

您似乎没有将URL附加到照片URL?请尝试以下操作:

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

nr9pn0ug4#

如果在使用SwiftUI创建的应用程序中创建图库选取器时选择了PHPicker,并且希望防止此选取器反复选择所选照片,则可以应用以下方法。
防止用户两次选择同一张照片的功能是**“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

相关问题