У меня есть функция в моем приложении, которая должна получать все изображения, хранящиеся в телефоне пользователей, по мере их прокрутки вниз, однако мне трудно это реализовать. пока что мой код получает только первые 30 изображений и повторяет это снова и снова, и я не знаю, как заставить его извлекать первые 30, а затем следующие 30 и так далее. Я попытался установить "photos.firstObject", чтобы указать последний объект массива изображений, но это говорит мне, что 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
}
cellForItemAt indexPath
- person Salman Ghumsani   schedule 06.10.2017