【问题标题】:Fetching the last image captured through iPhone camera获取通过 iPhone 相机拍摄的最后一张图像
【发布时间】:2011-08-10 05:27:51
【问题描述】:

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

【问题讨论】:

    标签: iphone ios4 camera uiimage


    【解决方案1】:

    您可以使用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];
    

    【讨论】:

    • ALAssetsLibraryChangedNotification 似乎只通知您所做的更改,这不太有用:stackoverflow.com/questions/3981161/…openradar.me/9018443哦,等等。现在它正在工作。打败我。
    • 另外,如果用户的照片库中有 0 张照片,那么上面的代码将在 iOS5.1 上崩溃(根据我的经验)。为避免这种情况,请添加 [group setAssetsFilter:[ALAssetsFilter allPhotos]];就在试图找出 numberOfAssets 之前。
    【解决方案2】:

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

    我使用这段代码让它工作了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多