如何从资产库iOS获取图像?

时间:2015-09-01 02:36:34

标签: ios objective-c

我是iOS开发人员我想从库中获取所有图像,没有UIImagepickercontroller,并拍摄前10张图片 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

有许多示例out der将指导您如何从ALAssetLibrary

获取图像

https://www.cocoacontrols.com/search?q=image+picker

以下是从ImagePicker

获取最新图片的示例
- (void)latestPhotoWithCompletion:(void (^)(UIImage *photo))completion
{

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

    // For this example, we're only interested in the last item [group numberOfAssets]-1 = last.
    if ([group numberOfAssets] > 0) {
        [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1] options:0
                             usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
                                 // The end of the enumeration is signaled by asset == nil.
                                 if (alAsset) {
                                     ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                                     // Do something interesting with the AV asset.
                                     UIImage *img = [UIImage imageWithCGImage:[representation fullScreenImage]];

                                     // completion
                                     completion(img);

                                     // we only need the first (most recent) photo -- stop the enumeration
                                     *innerStop = YES;
                                 }
                             }];
    }
} failureBlock: ^(NSError *error) {
    // Typically you should handle an error more gracefully than this.
}];


}

<强>用法

 __weak __typeof(self)wSelf = self;
    [self latestPhotoWithCompletion:^(UIImage *photo) {

        UIImageRenderingMode renderingMode = YES ? UIImageRenderingModeAlwaysOriginal : UIImageRenderingModeAlwaysTemplate;
        [wSelf.switchCameraBut setImage:[photo imageWithRenderingMode:renderingMode] forState:UIControlStateNormal];

    }];