处理相册分批加载:每次36

This commit is contained in:
bluesea 2024-03-25 16:55:36 +08:00
parent 8c9eda876f
commit e46743321d

View File

@ -133,15 +133,14 @@ class CCSpaceAlbumFilterPopView2: UIView {
var selectedVideoHandler: ((URL,PHAsset) -> Void)? var selectedVideoHandler: ((URL,PHAsset) -> Void)?
var selectedFileType:SelectedFileType = .all { var selectedFileType:SelectedFileType = .all {
didSet{ didSet{
filterOffset = 0
reloadData() reloadData()
} }
} }
var dataSource:[CCSpaceAlbumFliterModel] = [] var dataSource:[CCSpaceAlbumFliterModel] = []
var filterOffset = 0// let filterLimit = 36//
let filterLimit = 80// var isLoadingMoreData = false//
var shouldLoadDataCount = 0//
lazy var backView: UIView = { lazy var backView: UIView = {
let imageView = UIView(frame: CGRectMake(0, KScreenHeight + 10 , KScreenWidth, Back_Height)) let imageView = UIView(frame: CGRectMake(0, KScreenHeight + 10 , KScreenWidth, Back_Height))
@ -333,11 +332,13 @@ class CCSpaceAlbumFilterPopView2: UIView {
func reloadData(){ func reloadData(){
print("重新装载数据。。。") print("重新装载数据。。。")
self.dataSource.removeAll() self.dataSource.removeAll()
loadMoreData(offset: filterOffset) self.listCollectView.reloadData()
loadMoreData(offset: 0)
} }
func loadMoreData(offset:Int) { func loadMoreData(offset:Int) {
print("loadMoreData。。。")
isLoadingMoreData = true
let fetchResult_collection = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.any, options: nil); let fetchResult_collection = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.any, options: nil);
// //
var spatialAC:PHAssetCollection? var spatialAC:PHAssetCollection?
@ -361,9 +362,11 @@ class CCSpaceAlbumFilterPopView2: UIView {
} }
let fetchResult_asset = PHAsset.fetchAssets(in: sac, options: fetchOptions) let fetchResult_asset = PHAsset.fetchAssets(in: sac, options: fetchOptions)
guard offset < fetchResult_asset.count else {// guard offset < fetchResult_asset.count else {//
self.isLoadingMoreData = false
return return
} }
shouldLoadDataCount = min(fetchResult_asset.count - offset, filterLimit)
var count = 0 var count = 0
for i in offset ..< fetchResult_asset.count { for i in offset ..< fetchResult_asset.count {
let asset = fetchResult_asset.object(at: i) let asset = fetchResult_asset.object(at: i)
@ -374,6 +377,9 @@ class CCSpaceAlbumFilterPopView2: UIView {
} }
} }
} }
else {
isLoadingMoreData = false
}
self.listCollectView.reloadData() self.listCollectView.reloadData()
} }
@ -384,7 +390,6 @@ class CCSpaceAlbumFilterPopView2: UIView {
let requestOptions = PHImageRequestOptions() let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = false// requestOptions.isSynchronous = false//
requestOptions.deliveryMode = .highQualityFormat requestOptions.deliveryMode = .highQualityFormat
print("asset mediatype:\(asset.mediaType)")
PHImageManager.default().requestImageDataAndOrientation(for: asset, options: requestOptions) {[weak self] data, dataUTI, imagePropertyOrientation, info in PHImageManager.default().requestImageDataAndOrientation(for: asset, options: requestOptions) {[weak self] data, dataUTI, imagePropertyOrientation, info in
if let hasData = data { if let hasData = data {
let photoImage:UIImage = UIImage(data: hasData)! let photoImage:UIImage = UIImage(data: hasData)!
@ -392,6 +397,11 @@ class CCSpaceAlbumFilterPopView2: UIView {
let model = CCSpaceAlbumFliterModel.initWithAssetType(assetType: AssetType.init(rawValue: asset.mediaType.rawValue-1)!, asset: asset,image:photoImage, localIdentifier: asset.localIdentifier) let model = CCSpaceAlbumFliterModel.initWithAssetType(assetType: AssetType.init(rawValue: asset.mediaType.rawValue-1)!, asset: asset,image:photoImage, localIdentifier: asset.localIdentifier)
self?.dataSource.append(model) self?.dataSource.append(model)
self?.listReloadData() self?.listReloadData()
self?.shouldLoadDataCount = (self?.shouldLoadDataCount ?? 0) - 1
if (self?.shouldLoadDataCount ?? 0) <= 0 {
self?.isLoadingMoreData = false
}
} }
} }
@ -409,6 +419,26 @@ extension CCSpaceAlbumFilterPopView2:UICollectionViewDataSource, UICollectionVie
return self.dataSource.count return self.dataSource.count
} }
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let contentOffset = scrollView.contentOffset
let contentSize = scrollView.contentSize
let frameSize = scrollView.frame.size
//
if contentOffset.y + frameSize.height >= contentSize.height {
// collectionView
guard self.isLoadingMoreData == false else {
print("正在加载中,等加载完毕在装载更多。。。。")
return
}
self.loadMoreData(offset: self.dataSource.count)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: AlbumIdentifierCell, for: indexPath) as! CCSpaceAlbumFliterCollectionCell let cell = collectionView.dequeueReusableCell(withReuseIdentifier: AlbumIdentifierCell, for: indexPath) as! CCSpaceAlbumFliterCollectionCell