sony
sony

Reputation: 750

How to get image from asset library iOS?

I am iOS developer i want to get all images from library, without UIImagepickercontroller,and take first 10 images Any ideas?

Upvotes: 0

Views: 1018

Answers (1)

Mukesh
Mukesh

Reputation: 3690

There are lots of example out der which will guide you how to get images from ALAssetLibrary

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

Below is example to get the latest image from 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.
}];


}

Usage

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

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

    }];

Upvotes: 1

Related Questions