0

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).

Exothug
  • 339
  • 4
  • 18

4 Answers4

2

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

Arseniy
  • 304
  • 2
  • 5
1

@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

Arseniy
  • 304
  • 2
  • 5
  • i tried using an NSMutableArray but if i use the addObject method it returns null – Exothug Jul 18 '13 at 09:29
  • could you provide a simpler code sample for just retrieving the images ? – Exothug Jul 18 '13 at 09:32
  • @Exothug, the above is already the simplest code. if you're OK with storing all library images into an array, replace `// process the image here` with `dispatch_async(dispatch_get_main_queue(), ^{ [self.youImagesArray addObject:image]; });` – Arseniy Jul 18 '13 at 09:41
  • i don't get the array to work. if i try to log the array it returns (null) – Exothug Jul 18 '13 at 09:51
  • @Exothug, OK unless you figure out sooner I'll look to do a quick working GitHub project later today. ALAssetsLibrary API are OK but not the friendliest indeed, do remember spending a few days before figuring things out there :) – Arseniy Jul 18 '13 at 11:43
  • OK here is the project: [AssetLibraryPhotosViewer](https://github.com/akpw/AssetLibraryPhotosViewer) have not done an extensive testing, though does seem to work OK both on simulator and real device. – Arseniy Jul 18 '13 at 14:50
0

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];
];
Joakim Serholt
  • 403
  • 5
  • 17
0

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);
}];
Kumar KL
  • 15,315
  • 9
  • 38
  • 60