user3057414 user3057414 - 11 days ago 9
iOS Question

CollectionView do not reloading with DKImagePickerController

I'm trying to pick multiple images from the library, and view at the

UICollectionView
. Its work fine when picking the images, but the images is not appears on the
UICollectionView
. I try to select the images once again, and the previous images are viewed. The current selected is not. Here is the code:

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


Here is the 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

}

Answer

Try this,

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

Because asset.fetchOriginalImageWithCompleteBlock is async block.

Comments