Question

I've just started a new project where I want the user to be able to pick one of the images in the devices gallery.

I am trying to achieve this by using an ImageView and a UIStepper.

I want to write all images inside the gallery into an array and have the imageView navigate through the array with the + and - buttons of the stepper (selecting the current array position +1 or -1 depending on click).

Was it helpful?

Solution

OK as per prior discussion, here is the project: AssetLibraryPhotosViewer

Have not done an extensive testing, though does seem to run OK both on simulator and real device

OTHER TIPS

@Exothug, to give you an idea of how to enumerate the device library accessing full screen photos:

ALAssetsLibrary* assetLibrary = [[ALAssetsLibrary alloc] init];

[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if (group) {
        [group enumerateAssetsUsingBlock:^(ALAsset* asset, NSUInteger index, BOOL* innerstop) {
             if (asset) {
                 ALAssetRepresentation *rep = [asset defaultRepresentation];
                 CGImageRef iref = [rep fullScreenImage];
                 if (iref) {
                     UIImage *image = [UIImage imageWithCGImage:iref scale:rep.scale
                                                    orientation:(UIImageOrientation)rep.orientation];
                     // process the image here
                 }
             }
         }];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"failure: %@", [error localizedDescription]);
}];    

you can just process the image via adding it to your array, however depending on number of images in the library it might not be most effective. an alternative approach would be using images URL / indexes to iterate through the library, fetching the image from the library as its needed for display in your ImageView

Maybe try something like this, choose the directory if you want a specific group of images.

NSMutableArray *result = [NSMutableArray array];
 [[[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
    NSString *path = [obj lastPathComponent];
    [result addObject:path];
];

I thought that , You just need to retrieve the Photos of Camera Roll from Your device .

If so , Try with this :

ALAssetsLibrary

void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != NULL) {
            NSLog(@"See Asset: %@", result);
        }
    };

    void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
        }
    };

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock: ^(NSError *error) {
                             NSLog(@"Failure");
                         }];

OR

//Get camera roll images

- (void)updateLastPhotoThumbnail {
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    NSInteger numberOfAssets = [group numberOfAssets];
    if (numberOfAssets > 0) {
        NSLog(@"numberOfPictures: %d",numberOfAssets);
        //NSInteger lastIndex = numberOfAssets - 1;
        int i = 0;
        for (i = 0; i <= numberOfAssets-1; i++)  {
            [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
                NSLog(@"theObject!!!! -- (%d) %@",i,thumbnail);
                [cameraRollPictures addObject:thumbnail];
            }];
        }
    }
} failureBlock:^(NSError *error) {
    NSLog(@"error: %@", error);
}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top