处理相册分批加载:每次36
This commit is contained in:
parent
8c9eda876f
commit
e46743321d
@ -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
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user