Ios 如何在我的collectionView中获取“瞬间”相册和所有个人相册?

Ios 如何在我的collectionView中获取“瞬间”相册和所有个人相册?,ios,objective-c,uicollectionview,photosframework,Ios,Objective C,Uicollectionview,Photosframework,我正在尝试使用照片框架。在我的收藏视图中,我想显示相机卷和个人相册的海报图像 我试图以这种方式实现代码,但我仍然看到每个相册的相同图像。。。我哪里做错了 我把代码放在这里。。。我希望有人能帮助我 //USER ALBUM @property (nonatomic, strong) PHFetchResult *userAlbum; @property (nonatomic, strong) PHAssetCollection *assetCollection; @property (nonato

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

我试图以这种方式实现代码,但我仍然看到每个相册的相同图像。。。我哪里做错了

我把代码放在这里。。。我希望有人能帮助我

//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;
}
//用户相册
@属性(非原子,强)PHFetchResult*userAlbum;
@属性(非原子、强)相集合*资产集合;
@属性(非原子、强)相集*资产;
-(无效)viewDidLoad{
[超级视图下载];
PHFetchOptions*userAlbumsOptions=[[PHFetchOptions alloc]init];
userAlbumsOptions.sortDescriptors=@[[NSSortDescriptor SortDescriptor WithKey:@“creationDate”升序:是];
self.userAlbum=[PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum | PHAssetCollectionTypeMoment子类型:PhasSetCollectionSubtypeAlbum常规选项:nil];
对于(NSInteger i=0;i
您之所以看到相同的图像,是因为您正在为所有索引请求相同的资产:

-(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;
}
这样做:

self.assets = [PHAsset fetchKeyAssetsInAssetCollection:self.assetCollection options:nil];
[[PHImageManager defaultManager] requestImageForAsset:self.assets[indexPath.row]
                                           targetSize:square contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
   catCell.albumImage.image = result;
}];
3) 最后,不要这样做:

PHFetchResult *fetchResult = [PHAsset fetchKeyAssetsInAssetCollection:self.assetCollection options:nil];
self.asset = [fetchResult firstObject];
[[PHImageManager defaultManager] requestImageForAsset:self.asset
                                           targetSize:square contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
   catCell.albumImage.image = result;
}];
这样做:

self.assets = [PHAsset fetchKeyAssetsInAssetCollection:self.assetCollection options:nil];
[[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
)。因此,取而代之的是,为所有资产创建一个属性,并为正确的单元格获取正确的资产。

崩溃,我不知道为什么会发生什么样的崩溃..?愚蠢的错误。。。解决了的。。。打扰一下你的答案是正确的!。。谢谢你的支持!!!