Fetching the last image captured through iPhone camera
I am trying fetch the picture which is captured by the camera in the iPhone, programmatically. Now, the issue is I am using AVCaptureInput, and other AVFoundation headers and accessing the camera of iPhone instead of simple UIImagePickerViewController because, the program needs a small view inside the main view showing the camera footage. So Now, the issue is I need to fetch the last image I captured. It is being stored in camera roll folder inside library. I need to show it as a preview of last image captured - exactly as how the iPhone's camera d开发者_如何学编程oes.
You can use the AssetsLibrary
framework to access photos in the camera roll.
Something like this should work for getting the last image as a thumbnail:
- (void)updateLastPhotoThumbnail
{
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSInteger numberOfAssets = [group numberOfAssets];
if (numberOfAssets > 0) {
NSInteger lastIndex = numberOfAssets - 1;
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:lastIndex] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
if (thumbnail && thumbnail.size.width > 0) {
photoThumbnailView.image = thumbnail;
*stop = YES;
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(@"error: %@", error);
}];
}
This is assuming that you have assetsLibrary initialized as an instance variable. You can then also observe the notification that is posted when the library changes (could also happen outside of your app):
assetsLibrary = [[ALAssetsLibrary alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLastPhotoThumbnail) name:ALAssetsLibraryChangedNotification object:nil];
For some reasons above answer is not working for me.
I got it working by using this code.
galleryButton is an instance variable for a uibutton.
- (void)createGalleryButton
{
NSMutableArray *assets = [[NSMutableArray alloc] init];
void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger
index, BOOL *stop) {
if(result != nil) {
UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
[assets addObject:thumbnail];
}
};
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:assetEnumerator];
}
if(assets.count!=0)
{
UIImage *lastImage = (UIImage *)[assets lastObject];
[self.galleryButton setImage:lastImage forState:UIControlStateNormal];
}
else
{
[self.galleryButton setImage:[UIImage imageNamed:@"camera.bundle/camera-library.png"] forState:UIControlStateNormal];
}
};
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(@"Failure");
}];
}
to use it
self.galleryButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.galleryButton setFrame:CGRectMake(260, self.view.frame.size.height - 60, 50, 50)];
[self.galleryButton setImage:[UIImage imageNamed:@"camera.bundle/camera-library.png"] forState:UIControlStateNormal];
// assetsLibrary will take time getting all your images at this point. So performItWithDelay
[self performSelector:@selector(createGalleryButton) withObject:nil afterDelay:0.1];
[self.galleryButton addTarget:self action:@selector(showGallery:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.galleryButton];
精彩评论