1

Understand how to retrieve the last photo from iOS devices from link below

How to retrieve the most recent photo from Camera Roll on iOS?

But I wanted to retrieve the last 20 photos or so on as I do not want to slow down the performance as well as let user to look at their photos in reverse order instead

I tried

long index = group.numberOfAssets - 2;

and Ended up with this error

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSIndexSet initWithIndexesInRange:]: Range {4294967294, 1} exceeds maximum index value of NSNotFound - 1'

Anyone can help here? thanks

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    ***long index              = group.numberOfAssets - 2;***
    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:index]
                            options:0
                         usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
Community
  • 1
  • 1

1 Answers1

1

May be you enumerate in the wrong way. This code gets the last image

    ALAssetsGroup* group = [groups lastObject]; // get all assets groups, i think you know how to get them.
    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:(group.numberOfAssets - 1)] options:NSEnumerationConcurrent usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
     {
         if(result) {
                  // result is your needed last asset
             }

     }];
B.S.
  • 21,660
  • 14
  • 87
  • 109
  • I understand how to get the last photo. I trying to get the 2nd last photo but failed with that error. Not very sure about the NSindexSet limitation. – user1560963 Mar 07 '13 at 17:14
  • If you are sure that everything is ok with index, add *NSEnumerationConcurrent* option – B.S. Mar 07 '13 at 17:20
  • did anyone get perfect solution to this. – coder1010 Apr 24 '13 at 11:59
  • I use "enumerateAssetsWithOptions:NSEnumerationReverse" to extract photo backward and give those photos "index". Then I send them to the view and present as Thumbnail. Once user click on the Thumbnail photo. Photo will extract from Library base on the "index" that I tagged with. – user1560963 May 23 '13 at 02:40