获取通过iPhone相机捕获的最后一张图像

时间:2011-08-10 05:27:52

标签: iphone ios4 camera uiimage

我正在尝试以编程方式获取iPhone中相机捕获的图片。现在,问题是我正在使用AVCaptureInput和其他AVFoundation标头并访问iPhone的摄像头而不是简单的UIImagePickerViewController因为,程序需要在主视图内显示摄像机镜头的小视图。所以现在,问题是我需要获取我捕获的最后一个图像。它存储在库中的相机卷文件夹中。我需要将其显示为最后捕获的图​​像的预览 - 就像iPhone的相机一样。

2 个答案:

答案 0 :(得分:7)

您可以使用AssetsLibrary框架访问相机胶卷中的照片。

这样的东西可以用于将最后一个图像作为缩略图:

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

这假设您已将assetsLibrary初始化为实例变量。然后,您还可以观察库更改时发布的通知(也可能发生在应用程序之外):

assetsLibrary = [[ALAssetsLibrary alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLastPhotoThumbnail) name:ALAssetsLibraryChangedNotification object:nil];

答案 1 :(得分:0)

由于某些原因,上述答案对我不起作用。

我使用此代码完成了它。

galleryButton是一个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");
                                    }];
}

使用它

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