要从阵列中删除重复的PHAsset?

时间:2018-07-28 11:54:50

标签: arrays swift uiimagepickercontroller phasset

我正在使用名为BSImagePicker的第三方库从照片库中选择多张图像。我可以设置最多选择PHAssets,但是当我显示BSImagePickerController选择更多照片时,就会出现问题。那些PHAssets仅被添加到selectedAssets并实际上复制了图像。我尝试使用删除重复的PHAsset和图片,但无法正常工作:

extension Array where Element: Equatable {
mutating func removeDuplicates() {
    var result = [Element]()
    for value in self {
        if !result.contains(value) {
            result.append(value)
        }
    }
    self = result
   }
}

func presentBSImagePickerController(vc: BSImagePickerViewController) {
        self.bs_presentImagePickerController(vc, animated: true,
                                             select: { (asset: PHAsset) -> Void in

        }, deselect: { (asset: PHAsset) -> Void in
            // User deselected an assets.

        }, cancel: { (assets: [PHAsset]) -> Void in
            // User cancelled. And this where the assets currently selected.

        }, finish: { (assets: [PHAsset]) -> Void in


            // User finished with these assets

            for i in 0..<assets.count  {
                self.selectedAssets.append(assets[i])
            }
            self.selectedAssets.removeDuplicates()

            self.convertAssetToImages()

        }, completion: nil)
    }

    func convertAssetToImages() -> Void {

        if selectedAssets.count != 0 {

            for i in 0..<selectedAssets.count {

                let manager = PHImageManager.default()
                let option = PHImageRequestOptions()
                var thumbnail = UIImage()
                option.isSynchronous = true

                manager.requestImage(for: selectedAssets[i], targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: option, resultHandler: {(result, info)->Void in
                    thumbnail = result!

                })

                let data = UIImageJPEGRepresentation(thumbnail, 0.7)
                let newImage = UIImage(data: data!)

                self.photosArray.append(newImage! as UIImage)
                self.photosArray.removeDuplicates()
                print(self.photosArray.removeDuplicates())

                DispatchQueue.main.async {
                    self.collectionView.reloadData() // reloads the collection view on main thread
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

问题

for i in 0..<assets.count  {
    self.selectedAssets.append(assets[i])
}
self.selectedAssets.removeDuplicates()

...是您的removeDuplicates(除了令人无法忍受的缓慢!)只是假设PHAsset相等是复制图像的标记。不是。资产只是图书馆内容的临时代表。您想知道的是资产的本地标识符是否与您已经拥有的资产的本地标识符匹配。我建议:

var ids = Set(self.selectedAssets.map {$0.localIdentifier})
for asset in assets  {
    if !ids.contains(asset.localIdentifier) {
        ids.insert(asset.localIdentifier)
        self.selectedAssets.append(asset)
    }
}

我们列出了我们已经拥有的资产的所有本地标识符。然后,仅当资产(及其标识符)不在列表中时,我们才会附加资产。这将快得多(因为Set contains比Array contains快得多),并且还应防止“同一张照片”出现两次。

答案 1 :(得分:0)

步骤:1.此代码应足够:

extension Array where Element: Equatable {
mutating func removeDuplicates() {
    var result = [Element]()
    for value in self {
        if !result.contains(value) {
            result.append(value)
        }
    }
    self = result
}

}

步骤:2.“ selectedAssets”是PHAssets类型的数组,您必须从“ selectedAssets”数组中删除所有资产。因为要显示“ photosArray”中的multiImage,所以此数组始终具有您的图像,而“ selectedAssets”数组在删除后始终具有参考值。因此,请在for循环之前从“ selectedAssets”数组中删除所有资产,如下所示。或者,如果您有任何疑问,请随时向我提问(Mohit Tomer)

// MARK:- Selecting Multiple Image From Gallery.
func showingImagePicker() {
    let vc = BSImagePickerViewController()
    var ids = Set(self.selectedAssets.map {$0.localIdentifier})
    vc.maxNumberOfSelections = 5
    vc.cancelButton.tintColor = UIColor.red
    vc.selectionCharacter = "✓"

    self.bs_presentImagePickerController(vc, animated: true, select: { (asset: PHAsset) -> Void in
        print("Selected: \(asset)")
    }, deselect: { (asset: PHAsset) -> Void in
        print("Deselected: \(asset)")
    }, cancel: { (assets: [PHAsset]) -> Void in
        print("Cancel: \(assets)")
    }, finish: { (assets: [PHAsset]) -> Void in
        print("Finish: \(assets)")

        self.selectedAssets.removeAll()
        for asset in assets  {
            if !ids.contains(asset.localIdentifier) {
                ids.insert(asset.localIdentifier)
                self.selectedAssets.append(asset)
            }
        }
        self.convertingAssetToImage()

    }, completion: nil)
}




    func convertingAssetToImage() -> Void {
    var thumbnail = UIImage()
    if selectedAssets.count != 0 {

        for item in selectedAssets {
            let manager = PHImageManager.default()
            let option = PHImageRequestOptions()
            option.isSynchronous = true
            manager.requestImage(for: item, targetSize: CGSize(width: 200.0, height: 200.0), contentMode: .aspectFill, options: option, resultHandler: { (result, info) -> Void in
                print("result", result as Any)
                print("info", info as Any)
                thumbnail = result!
            })

            self.photoArray.append(thumbnail)
            self.photoDataArray.append(thumbnail.jpegData(compressionQuality: 0.5))

        }

    }
}