Created
June 15, 2013 08:19
-
-
Save danmartyn/5787376 to your computer and use it in GitHub Desktop.
Get the Last Photo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Link Against Assets Library | |
// #import <AssetsLibrary/AssetsLibrary.h> | |
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]]; | |
if (group) | |
{ | |
if ([group numberOfAssets] > 0) | |
{ | |
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) | |
{ | |
if (asset) | |
{ | |
ALAssetRepresentation *representation = [asset defaultRepresentation]; | |
ALAssetOrientation orientation = [representation orientation]; | |
UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:[representation scale] orientation:(UIImageOrientation)orientation]; | |
// resize the photo | |
latestPhoto = [latestPhoto resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:self.imagePickerView.frame.size interpolationQuality:kCGInterpolationHigh]; | |
// post a notification with the image | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"User Chose Image For Image View Element" object:self.imagePickerView userInfo:@{@"image":latestPhoto}]; | |
*stop = YES; | |
} | |
}]; | |
} else | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Photos" | |
message:@"It appears you do not have any photos" | |
delegate:nil | |
cancelButtonTitle:@"Okay" | |
otherButtonTitles:nil, nil]; | |
[alert show]; | |
} | |
} | |
} failureBlock: ^(NSError *error) { | |
// Typically you should handle an error more gracefully than this. | |
NSLog(@"error: %@", error); | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment