Get last image from Photos.app? Get last image from Photos.app? ios ios

Get last image from Photos.app?


This code snippet will get the latest image from the camera roll (iOS 7 and below):

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {    // Within the group enumeration block, filter to enumerate just photos.    [group setAssetsFilter:[ALAssetsFilter allPhotos]];    // Chooses the photo at the last index    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {        // The end of the enumeration is signaled by asset == nil.        if (alAsset) {            ALAssetRepresentation *representation = [alAsset defaultRepresentation];            UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];            // Stop the enumerations            *stop = YES; *innerStop = YES;            // Do something interesting with the AV asset.            [self sendTweet:latestPhoto];        }    }];} failureBlock: ^(NSError *error) {    // Typically you should handle an error more gracefully than this.    NSLog(@"No groups");}];

iOS 8 and above:

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];PHAsset *lastAsset = [fetchResult lastObject];[[PHImageManager defaultManager] requestImageForAsset:lastAsset                                          targetSize:self.photoLibraryButton.bounds.size                                         contentMode:PHImageContentModeAspectFill                                             options:PHImageRequestOptionsVersionCurrent                                       resultHandler:^(UIImage *result, NSDictionary *info) {                                           dispatch_async(dispatch_get_main_queue(), ^{                                               [[self photoLibraryButton] setImage:result forState:UIControlStateNormal];                                           });                                       }];


Great answer from iBrad, worked almost perfectly for me. The exception being that it was returning images at their original orientation (eg. upside down, -90°, etc).

To fix this I simply changed fullResolutionImage to fullScreenImage.

Here:

UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

It now works a treat.


iBrad's example includes an iOS8 snippet that apparently works, but I found myself confused by the return type he described. Here is a snippet that grabs the last image, including options for version and size requirements.

Of note are the ability to request a specific version (original, current) and size. In my case, as I wish to apply the returned image to a button, I request it sized and scaled to fit the button I'm applying it to:

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];PHAsset *lastAsset = [fetchResult lastObject];[[PHImageManager defaultManager] requestImageForAsset:lastAsset                                          targetSize:self.photoLibraryButton.bounds.size                                         contentMode:PHImageContentModeAspectFill                                             options:PHImageRequestOptionsVersionCurrent                                       resultHandler:^(UIImage *result, NSDictionary *info) {                                           dispatch_async(dispatch_get_main_queue(), ^{                                               [[self photoLibraryButton] setImage:result forState:UIControlStateNormal];                                           });                                       }];