Hi Brian
When i choose the photoselector button the images sometimes shuffle out of place once i cancel and click it again please help. Here is my code
var selectedImage: UIImage?
var images = [UIImage]()
var assets = [PHAsset]()
fileprivate func assetFetchOptions() -> PHFetchOptions {
let fetchOptions = PHFetchOptions()
fetchOptions.fetchLimit = 30
let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
fetchOptions.sortDescriptors = [sortDescriptor]
return fetchOptions
}
fileprivate func fetchPhotos(){
let allPhotos = PHAsset.fetchAssets(with: .image, options: assetFetchOptions())
DispatchQueue.global(qos: .background).async {
allPhotos.enumerateObjects({ (asset, count, stop) in
let imageManager = PHImageManager.default()
let targetsize = CGSize(width: 200, height: 200)
let options = PHImageRequestOptions()
options.isSynchronous = true
imageManager.requestImage(for: asset, targetSize: targetsize, contentMode: .aspectFit, options: nil, resultHandler: { (image, info) in
if let image = image{
self.images.append(image)
self.assets.append(asset)
if self.selectedImage == nil{
self.selectedImage = image
}
}
if count == allPhotos.count - 1{
DispatchQueue.main.async {
self.collectionView?.reloadData()
}
}
})
})
}
}