首页 > 解决方案 > 快速图像选择器获取多张图片

问题描述

我在使用 ImagePicker 将图像插入 CollectionView 单元格时遇到问题。问题是,如果我单击要快速插入几次的照片,则该照片会在不同的单元格中插入多次。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    picker.dismiss(animated: true, completion: nil)
    picker.allowsEditing = true
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

        collectionViewController?.imageArray.append(pickedImage)
        collectionViewController?.newImages.append(pickedImage)
        let guid = UUID()
        let fileManager = FileManager.default
        let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("\(guid).png")
        let data = UIImagePNGRepresentation(fixOrientation(img: pickedImage))
        fileManager.createFile(atPath: imagePath as String, contents: data, attributes: nil)
        let image = CoreDataManager.shared.createImageyObject(pfad: imagePath, date: Date(), guid: guid)
        collectionViewController?.images.append(image)

        if collectionViewController?.images.count == 1 {

            collectionViewController?.images[0].isBaseImage = true
            collectionViewController?.lastIndex = 0
        }

        collectionViewController?.update()

    }
}

标签: swift

解决方案


Mediainfo 包含图像 url,媒体 url ...也许您应该在 Set 中缓存选择的 imageURL(唯一的),然后检查 imageURL 是否已经存在。


推荐阅读