我试图使用Photos Framework
。在我的collectionView
中,我想显示Camera Roll
和Personal albums
的海报图片。
我试图以这种方式实现代码,但我仍然看到每张专辑的相同图片......我在哪里做错了?
我在这里放置代码......我希望有人可以帮助我
//USER ALBUM
@property (nonatomic, strong) PHFetchResult *userAlbum;
@property (nonatomic, strong) PHAssetCollection *assetCollection;
@property (nonatomic, strong) PHAsset *asset;
-(void)viewDidLoad {
[super viewDidLoad];
PHFetchOptions *userAlbumsOptions = [[PHFetchOptions alloc] init];
userAlbumsOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
self.userAlbum = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum | PHAssetCollectionTypeMoment subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (NSInteger i =0; i < self.userAlbum.count; i++) {
self.assetCollection = [self.userAlbum objectAtIndex:i];
NSLog(@"%@",[self.assetCollection.localizedTitle uppercaseString]);
PHFetchResult *fetchResult = [PHAsset fetchKeyAssetsInAssetCollection:self.assetCollection options:nil];
self.asset = [fetchResult firstObject];
}
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.userAlbum.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
KCCategoryCell *catCell = [collectionView dequeueReusableCellWithReuseIdentifier:categoryCellID forIndexPath:indexPath];
CGFloat retina = [UIScreen mainScreen].scale;
CGSize square = CGSizeMake(catCell.albumImage.bounds.size.width * retina, catCell.bounds.size.height * retina);
[[PHImageManager defaultManager] requestImageForAsset:self.asset targetSize:square contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
catCell.albumImage.image = result;
}];
return catCell;
}
答案 0 :(得分:1)
您之所以看到相同图片的原因是因为您要为所有索引请求相同的资源:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
KCCategoryCell *catCell = [collectionView dequeueReusableCellWithReuseIdentifier:categoryCellID forIndexPath:indexPath];
CGFloat retina = [UIScreen mainScreen].scale;
CGSize square = CGSizeMake(catCell.albumImage.bounds.size.width * retina, catCell.bounds.size.height * retina);
[[PHImageManager defaultManager] requestImageForAsset:self.asset // <-- Right here
targetSize:square contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
catCell.albumImage.image = result;
}];
return catCell;
}
所以不要这样做,不要这样做:
1)将@property (nonatomic, strong) PHAsset *asset;
更改为@property (nonatomic, strong) PHFetchResult *assets;
2)而不是这样做:
PHFetchResult *fetchResult = [PHAsset fetchKeyAssetsInAssetCollection:self.assetCollection options:nil];
self.asset = [fetchResult firstObject];
这样做:
self.assets = [PHAsset fetchKeyAssetsInAssetCollection:self.assetCollection options:nil];
3)最后,而不是这样做:
[[PHImageManager defaultManager] requestImageForAsset:self.asset
targetSize:square contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
catCell.albumImage.image = result;
}];
这样做:
[[PHImageManager defaultManager] requestImageForAsset:self.assets[indexPath.row]
targetSize:square contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
catCell.albumImage.image = result;
}];
回顾一下:
您为所有单元格一遍又一遍地获取相同的图像(self.asset
)。因此,相反,为所有资产创建一个属性,并为正确的单元格获取正确的资产。