在UICollectionView中显示照片库图像

时间:2015-12-22 13:47:12

标签: ios objective-c uicollectionview alassetslibrary

我正试图在UICollectionViewALAssetsLibrary中显示照片库中的图像我的代码运行正常,但我有一些问题。

  1. 缩略图的质量很差。
  2. 如何安排收藏视图通过订购显示100张近期照片 从
  3. 新旧。

    这是我的代码:

    - (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;
    }
    

2 个答案:

答案 0 :(得分:1)

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

  NSDate * date = [asset valueForProperty:ALAssetPropertyDate];

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

还有你的另一个问题,

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

你可以试试这个:

      [UIImage imageWithCGImage:[asset aspectRatioThumbnail]

答案 1 :(得分:1)

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

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

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

    return [date1 compare:date2];
}];