Ios 在UICollectionView中显示照片库图像
我正试图通过Ios 在UICollectionView中显示照片库图像,ios,objective-c,uicollectionview,alassetslibrary,Ios,Objective C,Uicollectionview,Alassetslibrary,我正试图通过ALAssetsLibrary在UICollectionView中显示照片库中的图像,我的代码运行正常,但我遇到了一些问题 缩略图的质量很差 如何安排收藏视图通过订购显示100张最近的照片 从 从新到旧 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // co
ALAssetsLibrary
在UICollectionView
中显示照片库中的图像,我的代码运行正常,但我遇到了一些问题
- (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;
}
您可以通过以下方式获取保存在库中的图像的日期:
NSDate * date = [asset valueForProperty:ALAssetPropertyDate];
您可以将此日期与今天的日期进行比较,并将其存储为一个数组,然后对100幅图像执行相同的操作
至于你的另一个问题
根据iOS,您从asset获得的缩略图img大小不同。在iOS 9中为75x75,在iOS 8中为150x150
您可以尝试以下方法:
[UIImage imageWithCGImage:[asset aspectRatioThumbnail]
使用以下代码对照片进行排序
self.photos = [collector sortedArrayUsingComparator:^NSComparisonResult(ALAsset *first, ALAsset *second) {
NSDate * date1 = [first valueForProperty:ALAssetPropertyDate];
NSDate * date2 = [second valueForProperty:ALAssetPropertyDate];
return [date1 compare:date2];
}];
使用[asset valueForProperty:ALAssetPropertyDate]将照片从新到旧进行排序。您是否将img转换为cgi img?这可能是质量差的原因吗?@SuryaSubenthiran我应该把这个代码放在哪里?视图加载了吗?请再具体一点好吗?thanks@Mc.Lover.I他补充道。请看一看。谢谢,由于
***由于未捕获的异常“NSUnknownKeyException”终止应用程序,它崩溃了,原因:“[valueForUndefinedKey:]:该类不符合密钥ALAssetPropertyDate的键值编码。”***第一次抛出调用堆栈:
@Mc.Lover我已经编辑了答案。请看一看,没什么变化!它运行正常,但我仍然看到了旧图像我必须向下滚动才能看到新的onestry[date2 compare:date1]我还可以知道您是否对imgWithCGi进行了两次转换?