我正在尝试创建一个具有上传图片功能的应用。我想创建一个加载所有图像的屏幕,该图像可能是照片库中的1000多个图像。然后将所有这些加载到“收集”视图中,并按日期进行排序和分组。
我尝试了批量加载,但无法正常工作,它仅加载第一批。还有其他方法可以做到这一点吗?我可以参考任何教程吗?
var myCollectionView: UICollectionView!
var photosDict = Dictionary<String,[Photo]>()
var photoAssets: PHFetchResult<PHAsset>?
let itemsPerBatch = 20
var batchNumber = 0
func grabPhotos() {
DispatchQueue.global(qos: .background).async {
let imgManager=PHImageManager.default()
let requestOptions=PHImageRequestOptions()
requestOptions.isSynchronous=true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions=PHFetchOptions()
fetchOptions.sortDescriptors=[NSSortDescriptor(key:"creationDate", ascending: false)]
self.photoAssets = PHAsset.fetchAssets(with: .image, options: fetchOptions)
self.loadMorePhotos(imgManager: imgManager, requestOptions: requestOptions)
self.batchNumber = self.batchNumber + 1
}
}
func loadMorePhotos(imgManager: PHImageManager, requestOptions: PHImageRequestOptions) {
photosDict.removeAll()
let formatter = DateFormatter()
formatter.dateFormat = "dd-MMM-yyyy"
if let photoAssetsUW = self.photoAssets, photoAssetsUW.count > 0 {
print(photoAssetsUW)
print(photoAssetsUW.count)
for i in (self.batchNumber * self.itemsPerBatch)..<(self.itemsPerBatch * (self.batchNumber + 1)) {
if i < photoAssetsUW.count {
let photoAsset = photoAssetsUW.object(at: i)
if let creationDate = photoAsset.creationDate {
let stringDate = formatter.string(from: creationDate)
var imageURL: String = ""
photoAsset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (eidtingInput, info) in
if let input = eidtingInput, let imgURL = input.fullSizeImageURL {
imageURL = imgURL.absoluteString
}
}
imgManager.requestImage(for: photoAssetsUW.object(at: i) , targetSize: CGSize(width:500, height: 500),contentMode: .aspectFill, options: requestOptions, resultHandler: { (image, error) in
if let imageUW = image {
if self.photosDict[stringDate] == nil {
self.photosDict[stringDate] = [Photo(image: imageUW,imagePath: imageURL)]
} else {
self.photosDict[stringDate]?.append(Photo(image: imageUW,imagePath: imageURL))
}
}
})
}
}
}
} else {
print("You got no photos.")
}
print("photosDict count: \(self.photosDict.count) \(self.photosDict)")
DispatchQueue.main.async {
self.myCollectionView.reloadData()
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let keys = Array(photosDict.keys).sorted(by: <)
let currentKey = keys[indexPath.section]
let imageList = photosDict[currentKey]
if indexPath.row == imageList?.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PhotoItemCell
let requestOptions=PHImageRequestOptions()
requestOptions.isSynchronous=true
requestOptions.deliveryMode = .highQualityFormat
loadMorePhotos(imgManager: PHImageManager.default(), requestOptions: requestOptions)
return cell
}
let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PhotoItemCell
cell.img.image = imageList?[indexPath.row].image
return cell
}
我想创建一个看起来与Apple Photos应用程序完全一样的屏幕,并加载来自该应用程序的所有图像和视频。