0

I'm working on photo app and here is the deal:

I have created custom photo album an added some photos to it (users can create many albums). I used ALAssetsLibrary for this and CustomPhotoAlbum category. Everything worked flawlessly until I needed to get those photos back to reuse them. So Ive created custom metod for this category and I'm saving UIImages to NSMutableArray but I want this method to return this array outside... My problem is that enumerateAssetsUsingBlock: is running in background and saving photos to album while I want to access them! How can I do something for this method to wait until every photo is loaded to album and then return NSArray?

Here is my method:

 - (void)loadImagesFromAlbumNamed:(NSString *)name
{
    NSMutableArray *photos = [[NSMutableArray alloc] init];

    [self enumerateGroupsWithTypes:ALAssetsGroupAll
                        usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                            if (group == nil) {
                                return;
                            }

                            if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:name]) {
                                [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                                    if (result == nil) {
                                        return;
                                    }

                                    // NSLog(@"%@", result);
                                    UIImage *image = [UIImage imageWithCGImage:(__bridge CGImageRef)([result defaultRepresentation])];
                                    [photos addObject:image];
                                }];
                                NSLog(@"%@", [photos description]);
                            }
                        } failureBlock:^(NSError *error) {
                            NSLog(@"%@", [error localizedDescription]);
                        }];
}

I hope you get my point of view... If not, just ask and I'll try to explain it better way!

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
cojoj
  • 6,405
  • 4
  • 30
  • 52
  • I've managed how to get an `NSArray` outside (passing destination array by param) but It's still empty when I want to get `UIImages` from it... – cojoj Jul 30 '13 at 09:36
  • Hey I am dealing with the same problem..can u tell me what is enumerateGroupsWithTypes ?....it is showing error at that line.. – Prashant Nikam Aug 06 '13 at 12:22
  • `enumerateGroupsWithTypes` is method which enumerates through every AssetLibrary available on your device... So in my case it's enumerating everything. – cojoj Aug 06 '13 at 15:04
  • ok..got that...but did u find any solution for ur problem...? the above code work fine and fetch images properly but how can we wait till it gets executed completed..and then return the imageArray ? – Prashant Nikam Aug 07 '13 at 03:36
  • The one I found workig for me is to wait... or if you want to do this automatically you shoud put `loadImagesFromAlbumNamed` into the completion block of `saveImage:toAlbum:withCompletionBlock:`. Let me know if that worked for you. – cojoj Aug 07 '13 at 07:55
  • Hey...Images are coming in the array but when I try to add them on View nothing is coming..! Just Blank images. – Prashant Nikam Aug 07 '13 at 12:54
  • I'm stuck here too, bro... – cojoj Aug 07 '13 at 17:47

1 Answers1

1

Don't try to wait. Instead, pass a block to your method which should be called when all of the asset processing blocks are completed and will pass the array of images back to the caller. The caller should not block, you just want to structure your method to deal with the asynchronous nature of what you're trying to do.

Wain
  • 118,658
  • 15
  • 128
  • 151