【问题标题】:Displaying photo library images in UICollectionView在 UICollectionView 中显示照片库图像
【发布时间】:2015-12-22 13:47:11
【问题描述】:

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

  1. 缩略图的质量很差。
  2. 如何通过排序排列收藏视图显示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;
}

【问题讨论】:

  • 使用 [asset valueForProperty:ALAssetPropertyDate] 将照片从新到旧排序。
  • y r u 将 img 转换为 cgi img 吗?这可能是质量差的可能原因吗?
  • @SuryaSubenthiran 我应该把这段代码放在哪里?查看是否加载?请更具体一点吗?谢谢
  • @Mc.Lover.I 添加了答案。请看一看。

标签: ios objective-c uicollectionview alassetslibrary


【解决方案1】:

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

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

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

    return [date1 compare:date2];
}];

【讨论】:

  • 谢谢,由于*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ALAsset 0x7f9bb3c33fa0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key ALAssetPropertyDate.' *** First throw call stack:而崩溃
  • @Mc.Lover 我已经编辑了答案。请看一下。
  • 没有任何改变!它运行良好,但我仍然看到旧图像我必须向下滚动才能看到新图像
  • 试试 [date2 compare:date1]
【解决方案2】:

您可以通过以下方式获取图片库中保存的日期:

  NSDate * date = [asset valueForProperty:ALAssetPropertyDate];

您可以将此日期与今天的日期进行比较,并将其存储为一个数组,并对 100 个图像执行相同操作。

关于你的另一个问题,

您从资产中获得的缩略图图像大小不同,具体取决于 iOS。在 iOS 9 中为 75x75,在 iOS 8 中为 150x150。

你可以试试这个:

      [UIImage imageWithCGImage:[asset aspectRatioThumbnail]

【讨论】:

  • 另外我想知道你是在做两次imgWithCGi的转换吗?
猜你喜欢
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 2020-08-11
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多