0

I've written the following code (iOS 6) to try to recover the list of photos from an album (albumName) using ALAssetLibrary. The code compiles and according to the static analyzer I don't have memory leaks or other issues, but the NSLog statement. The code successfuly finds the photo album and if I break inside the loop I can see that the data are being written to the Dictionary. But they are not being emplaced as objects in the Array!

I am sure this is an oversight on my part but I am not able to spot it. Any help appreciated, and maybe the correct answer will help someone else. AlAssets has been difficult for me!

Thanks in advance

TF Redfield

  • (NSMutableArray )retrieveImageNames: (NSString)albumName {

ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init]; NSMutableArray* assetGroups = [[NSMutableArray alloc] init];

[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
    if (group)
    {
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
        {
            if (asset)
            {
                //compare the names of the albums
                if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame)
                {
                    NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init] ;
                    [workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
                    [workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
                    [workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];

                    [assetGroups addObject:workingDictionary];

                    //If I place the log here I get data
                    //NSString * temp = [NSString stringWithFormat:@"%d", [assetGroups count]];
                    //NSLog(@"%@",temp);

                    [workingDictionary release];
                }

            }
        }];
    }
} failureBlock:^(NSError *error)
{
    NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];

NSString * temp = [NSString stringWithFormat:@"%d", [assetGroups count]];
NSLog(@"%@",temp);

return assetGroups;
[assetsLibrary release];
[assetGroups release];

}

2
  • To clarify: the NSLog returns proper values when called inside the loop, but it returns zero for the count when at the bottom where the return should be made, and the array is empty. Commented Aug 27, 2013 at 19:20
  • Hi -- am still struggling with this. one observation is that if I fill the array with a dummy value (a string or whatever) right after allocating it, before the first block is called, it will be returned from the method as a valid string. But if I fill it within the block (e.g. right after the AssetLibrary is enumerated) it will return as empty. Any leads appreciated... Commented Sep 3, 2013 at 19:09

1 Answer 1

0

enumerateGroupsWithTypes is an asynchronous call. You have returned your array before it gets filled.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.