【问题标题】:How can I get the Moment album and all the personal albums in my collectionView?如何在我的 collectionView 中获取 Moment 相册和所有个人相册?
【发布时间】:2017-12-07 18:22:00
【问题描述】:

我正在尝试使用Photos Framework。在我的collectionView 中,我想展示Camera RollPersonal 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;
}

【问题讨论】:

    标签: ios objective-c uicollectionview photosframework


    【解决方案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)。因此,改为为所有资产创建一个属性,并为正确的单元格获取正确的资产。

    【讨论】:

    • 崩溃,我不知道为什么
    • 什么样的崩溃..?
    • 愚蠢的错误...已解决...对不起...您的答案是正确的!..感谢您的支持!!!
    猜你喜欢
    • 2012-03-31
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2011-08-08
    相关资源
    最近更新 更多