CollectionView不会使用DKImagePickerController重新加载

时间:2016-11-23 05:49:54

标签: ios image uicollectionview

我正在尝试从库中选择多个图像,并在UICollectionView处查看。选择图像时它的工作正常,但图像不会出现在UICollectionView上。我尝试再次选择图像,并查看以前的图像。当前选择的不是。

以下是代码:

imagePicker.didSelectAssets = {[unowned self] (assets: [DKAsset]) in
    print("Selected!")

    for asset in assets {
        asset.fetchOriginalImageWithCompleteBlock({ (image, info) in
            guard let imageData = UIImageJPEGRepresentation(image!, 0) else {
                print("There is no image bro..!")
                return
            }
            let thumbnails = UIImage(data: imageData)
            self.imageArray.append(thumbnails!)
            print(image!)
        })
    }

    self.collectionView.reloadData()
}

以下是cellForItemAt:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectedImage", for: indexPath) as! SelectedImageCell
    cell.image = imageArray[indexPath.item]
    return cell

}

对于任何想知道我是如何解决问题的人。这是代码。

 let group = DispatchGroup()

        for asset in assets {
            group.enter()
            asset.fetchOriginalImageWithCompleteBlock({ (image, info) in

                guard let imageData = UIImageJPEGRepresentation(image!, 0) else {
                    print("There is no image bro..!")
                    return
                }
                let thumbnails = UIImage(data: imageData)
                self.imageArray.append(thumbnails!)
                print(image!)
                group.leave()
            })
        }

        group.notify(queue: .main, execute: {
            print("finish..")
            self.collectionView.reloadData()
        })

1 个答案:

答案 0 :(得分:1)

试试这个,

imagePicker.didSelectAssets = {[unowned self] (assets: [DKAsset]) in
        print("Selected!")

        for asset in assets {

            asset.fetchOriginalImageWithCompleteBlock({ (image, info) in

                guard let imageData = UIImageJPEGRepresentation(image!, 0) else {
                    print("There is no image bro..!")
                    return
                }
                let thumbnails = UIImage(data: imageData)
                self.imageArray.append(thumbnails!)
                print(image!)
                self.collectionView.reloadData()
            })
        }
    }

因为asset.fetchOriginalImageWithCompleteBlock是异步阻止。