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?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With