Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm having a hard figuring out how to programmatically retrieve the most recent photo in the camera roll without user intervention. To be clear, I do not want to use an Image Picker, I want the app to automatically grab the newest photo when the app opens.

I know this is possible because Ive seen a similar app do it, but I cant seem to find any info on it.

like image 257
user1023127 Avatar asked Apr 17 '12 23:04

user1023127


People also ask

How can I get the last photo off my iPhone?

Connect your new iPhone and open iTunes (or Finder if you are using macOS Catalina or higher). Select your device in iTunes or Finder > Click Photos > Check the option to Sync Photos > Select the folder on your computer you wish to copy photos from > Click Apply.

Can iphones sort recent photos?

You can filter and sort photos and videos in the albums you create in the Photos app . For example, you can filter an album to show only videos, only photos, or photos and videos you marked as favorites. You can also sort photos and videos in an album by newest to oldest, oldest to newest, or in a custom order.


1 Answers

One way is to use AssetsLibrary and use n - 1 as the index for enumeration.

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos                              usingBlock:^(ALAssetsGroup *group, BOOL *stop) {                                  if (nil != group) {                                      // be sure to filter the group so you only get photos                                      [group setAssetsFilter:[ALAssetsFilter allPhotos]];                                       if (group.numberOfAssets > 0) {                                          [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:group.numberOfAssets - 1]                                                                  options:0                                                               usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {                                                                   if (nil != result) {                                                                       ALAssetRepresentation *repr = [result defaultRepresentation];                                                                       // this is the most recent saved photo                                                                       UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];                                                                       // we only need the first (most recent) photo -- stop the enumeration                                                                       *stop = YES;                                                                   }                                                               }];                                      }                                  }                                   *stop = NO;                              } failureBlock:^(NSError *error) {                                  NSLog(@"error: %@", error);                              }]; 
like image 85
Art Gillespie Avatar answered Oct 01 '22 05:10

Art Gillespie