我的应用程序中有一个功能,它应该可以在用户手机中向下滚动时存储所有图像,但是我很难尝试实现它。到目前为止,我的代码只获得了前30个图像并反复重复,我不确定如何获取前30个然后下30个等等。 我试过设置" photos.firstObject"说出images数组的最后一个对象,但它告诉我firstObject是一个getOnly属性。在此先感谢,这是我的代码
func fetchPhotos() {
let assetfetch = PHFetchOptions()
assetfetch.fetchLimit = 30
let photos = PHAsset.fetchAssets(with: .image, options: assetfetch)
// calls the code in the background thread
DispatchQueue.global(qos: .background).async {
photos.enumerateObjects({ (asset, count, stop) in
// size of the fetched images
let imageManager = PHImageManager.default()
let targetSize = CGSize(width: 200, height: 200)
//print(asset.creationDate)
// display better quality thumbnail image
let options = PHImageRequestOptions()
options.isSynchronous = true
// gets the image and appends them to the arrays
imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in
if let image = image {
self.images.append(image)
self.assets.append(asset)
// sets the first image as the default selected image
if self.selectedImage == nil {
self.selectedImage = image
}
}
// when the photos count reaches 29, reload the collection view to fill the photos
if count == photos.count - 1 {
DispatchQueue.main.async {
self.collectionView?.reloadData()
}
}
})
})
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! photoSelectorCell
cell.imageView.image = images[indexPath.item]
if indexPath.item == self.images.count - 1 {
fetchPhotos()
}
return cell
}