gpt4 book ai didi

ios - 如何获取 Moment 相册以及我的 collectionView 中的所有个人相册?

转载 作者:行者123 更新时间:2023-11-29 00:07:05 25 4
gpt4 key购买 nike

我正在尝试使用照片框架。在我的collectionView中,我想显示相机胶卷个人相册的海报图像。

我正在尝试以这种方式实现代码,但我仍然看到每个专辑的相同图像......我在哪里做错了?

在这里我放置我的代码......我希望有人可以帮助我

//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;
}

最佳答案

您看到相同图像的原因是因为您为所有索引请求相同的 Assets :

-(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)。因此,改为为所有 Assets 创建一个属性,并为正确的单元格获取正确的 Assets 。

关于ios - 如何获取 Moment 相册以及我的 collectionView 中的所有个人相册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47701418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com