0
votes

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];

}

1
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. - user1976727
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... - user1976727

1 Answers

0
votes

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