gpt4 book ai didi

ios - 在 UICollectionView 中显示照片库图像

转载 作者:行者123 更新时间:2023-11-29 10:22:14 25 4
gpt4 key购买 nike

我正在尝试通过 ALAssetsLibraryUICollectionView 中显示照片库中的图像,我的代码运行良好,但我遇到了一些问题。

  1. 缩略图的质量很差。
  2. 如何安排收藏 View 按顺序显示 100 张最近的照片来自

从新到旧。

这是我的代码:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// collect the photos
NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];
ALAssetsLibrary *al = [ViewController defaultAssetsLibrary];
[al enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset) {
[collector addObject:asset];
}
}];

self.photos = collector;
}
failureBlock:^(NSError *error) { NSLog(@"error");}];


}



-(void)setPhotos:(NSArray *)photos {
if (_photos != photos) {
_photos = photos;
[_collectionView reloadData];
}
}


+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _photos.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

UIImageView *collectionImageView = (UIImageView *)[cell viewWithTag:100];
ALAsset *asset = [self.photos objectAtIndex:indexPath.row];


UIImage* img = [UIImage imageWithCGImage:asset.thumbnail];
img = [UIImage imageWithCGImage:img.CGImage scale:2.0 orientation:UIImageOrientationUp];

[collectionImageView setImage:img];


return cell;
}

最佳答案

使用以下代码对照片进行排序。

self.photos = [collector sortedArrayUsingComparator:^NSComparisonResult(ALAsset *first, ALAsset *second) {

NSDate * date1 = [first valueForProperty:ALAssetPropertyDate];
NSDate * date2 = [second valueForProperty:ALAssetPropertyDate];

return [date1 compare:date2];
}];

关于ios - 在 UICollectionView 中显示照片库图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417334/

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