快速图像选择器获取几张图片

时间:2018-09-11 08:16:36

标签: swift

我在使用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()

    }
}

1 个答案:

答案 0 :(得分:0)

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