iOS.Lover
iOS.Lover

Reputation: 6051

Displaying photo library images in UICollectionView

I am trying to display images from Photo Library in UICollectionView through ALAssetsLibrary my codes runs fine , but I have some issues .

  1. The quality of thumbnails are poor .
  2. How can arrange collection view show 100 recent photos by ordering from

new to old .

here is my codes :

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

Upvotes: 1

Views: 1587

Answers (2)

Surya Subenthiran
Surya Subenthiran

Reputation: 2217

Use the following code to sort the photos.

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

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

    return [date1 compare:date2];
}];

Upvotes: 1

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

You can get the date of an image saved in the library by:

  NSDate * date = [asset valueForProperty:ALAssetPropertyDate];

You can compare this date with today's date, and store it an array, and do the same for the 100 Images.

And to your other question,

The thumbnail img you get from asset is of different size depends on iOS. In iOS 9 it is of 75x75 and in iOS 8, it is of 150x150.

You can try this:

      [UIImage imageWithCGImage:[asset aspectRatioThumbnail]

Upvotes: 1

Related Questions