10

I am trying to get all the images that are in the photos.app and display them in a UICollectionView. I have this code to retrieve the images:

ALAssetsLibrary *al = [ViewController defaultAssetsLibrary];

ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){

    if (result!=NULL) {

        if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {

            [imagesGotFromUserLibrary addObject:result];
        }

    }
};

ALAssetsLibraryGroupsEnumerationResultsBlock
libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    if (group!=nil)
    {

        [group enumerateAssetsUsingBlock:groupEnumerAtion];
    }
    else
    {

        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            GalleryCollectionViewController *otherController = [[GalleryCollectionViewController alloc] init];
            [otherController receiveImagesWithMutableArray:imagesGotFromUserLibrary];

        });
    }

};



al = [[ALAssetsLibrary alloc] init];
[al enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                       usingBlock:libraryGroupsEnumeration
                     failureBlock:^(NSError *error){
                         NSLog(@"ERROR: %@", error);
                     }];

This is in the viewDidLoad and then:

+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
    library = [[ALAssetsLibrary alloc] init];
});
return library;
}

This piece of code is sending an array to another controller that will set the images to my UICollectionView. The problem is that I am getting an error "invalid attempt to access past the lifetime of its owning ALAssetsLibrary" and if I try to NSLog my array the result is something like "ALAsset - Type:Unknown, URLs:(null)".

I looked up on the internet and I found a solution. I should add this line of code but it doesn't work. The code is:

+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
    library = [[ALAssetsLibrary alloc] init];
});
return library;
}

Anyone is able to help me on getting the correct images URLs to display?

BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43

2 Answers2

15

The solution is to always use the same library for all accesses to the assets across all of your classes. The singleton solution you show above is good - so long as all of your classes call defaultAssetsLibrary and none of them alloc/init their own ALAssetsLibrary.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks, but this unfortunately didn't help me. I am only passing an array to another class. I access to the ALAssetsLibrary only once and I can't understand whats' wrong. I don't think I am creating another instance of it but maybe I am wrong. Can you please double check my code? :) – BalestraPatrick Jun 09 '13 at 07:35
  • 2
    You need to move the defaultAssetsLibrary method somewhere common so that the library isn't destroyed. The app delegate, or the first view controller (if it pushes the second view controller which chooses the assets and sends them back to the first). – Wain Jun 09 '13 at 07:48
1

I have the same problem. And I finally solved it.

This is my condition: I have two controllers, the first one push to the second. In the first one I initialized library using following code:

let library = ALAssetsLibrary()

and I put all group information into an array, so that I can use them in the second view controller. But this may lead to no photo in group.

and the error message is the same as yours.

This is because the library is released by system. And all photo information is only valid in the lifetime of its owning ALAssetsLibrary. So the group.numberOfAssets() will be 0.

The solution is avoid library be released before the first controller release. So I use the following code:

static let library = ALAssetsLibrary()
firstViewController.library.enmurate.....balabala

Hope this may help someone.

JZAU
  • 3,550
  • 31
  • 37