如何获取我的collectionView中的朋友圈相册和所有个人相册?
How can I get the Moment album and all the personal albums in my collectionView?
我正在尝试使用 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;
}
您看到相同图片的原因是您为所有索引请求相同的资产:
-(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
)。因此,相反,为所有资产创建一个 属性 并为正确的单元格获取正确的资产。
我正在尝试使用 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;
}
您看到相同图片的原因是您为所有索引请求相同的资产:
-(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
)。因此,相反,为所有资产创建一个 属性 并为正确的单元格获取正确的资产。