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

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

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);                             }];

Instead of messing with the index, you can enumerate through the list in reverse. This pattern works well if you want the most recent image or if you want to list the images in a UICollectionView with the most recent image first.

Example to return the most recent image:

[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {    if (asset) {        ALAssetRepresentation *repr = [asset defaultRepresentation];        UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];        *stop = YES;    }}];

In iOS 8, Apple added the Photos library which makes for easier querying. In iOS 9, ALAssetLibrary is deprecated.

Here's some Swift code to get the most recent photo taken with that framework.

import UIKitimport Photosstruct LastPhotoRetriever {    func queryLastPhoto(resizeTo size: CGSize?, queryCallback: (UIImage? -> Void)) {        let fetchOptions = PHFetchOptions()        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]//        fetchOptions.fetchLimit = 1 // This is available in iOS 9.        if let fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) {            if let asset = fetchResult.firstObject as? PHAsset {                let manager = PHImageManager.defaultManager()                // If you already know how you want to resize,                 // great, otherwise, use full-size.                let targetSize = size == nil ? CGSize(width: asset.pixelWidth, height: asset.pixelHeight) : size!                // I arbitrarily chose AspectFit here. AspectFill is                 // also available.                manager.requestImageForAsset(asset,                    targetSize: targetSize,                    contentMode: .AspectFit,                    options: nil,                    resultHandler: { image, info in                    queryCallback(image)                })            }        }    }}