When I use ALAssetsLibrary to get local photos it works fine. But after I delete some photos with the \'Photos\' application my app crashes.
Crash info is:
What you need to is register an observer for ALAssetsLibraryChangedNotification
to get library changes. When the notification fires, reenumerate the groups and content of the AssetsLibrary. If you don't register for the notification your application will get an old snapshot of the library and enumeration will fail.
Please also note that there is a bug regarding ALAssetsLibraryChangedNotification
under iOS 5.x as documented here: http://www.openradar.me/10484334
ALAssetsLibrary library is depriciated on PHAssetsLibrary so use this code:
__block PHAssetCollection *collection;
_arr_downloadedWallpaper = [[NSMutableArray alloc] init];
// Find the album
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", @"Bhakti Ras"];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAny
options:fetchOptions].firstObject;
PHFetchResult *collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
[collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
//add assets to an array for later use in the uicollectionviewcell
NSLog(@"asset is =%@",asset);
if (asset) {
[self.arr_downloadedWallpaper addObject:asset];
}
}];
This seems to be an iOS bug, like you said ALAssetsLibrary returned the wrong number of your photos so you got index out of bounds error. The workaround is to reload your photo again like these:
ALAssetsLibraryGroupsEnumerationResultsBlock
libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop)
{
if (group == nil)
{
return;
}
//Force to reload photo as numberOfAssets is broken
NSLog(@"how many picture I have in this group: %d",[group numberOfAssets]);
[group setAssetsFilter:[ALAssetsFilter allPhotos]];//this will cause group to reload
NSLog(@"how many picture I have in this group: %d",[group numberOfAssets]);
if (group!=nil) {
[group enumerateAssetsUsingBlock:groupEnumerAtion];
}
[self updatephotoList];
};
I started out with Qiulang answer, but it didn't work for me. What worked for me is to call the setAssetsFilter 3 times in a row with all filter combinations, before starting the enumeration.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group setAssetsFilter:[ALAssetsFilter allAssets]];
At iOS 8 I noticed also that numberOfAssets returns wrong number of photos if some of them were deleted and located at 'Recently Deleted' album currently.
Registering an observer did not help in my case. Users were still crashing, but I didn't. Until today.
I figured out the way to solve this crash. It's a bug in Apple's photo library in the end, but there's a workaround. What you do is, you set the filter to photo, and then to video, instead of leaving it at the default 'assets'. You then enumerate it once for each, and do some trickery to ensure you get that final 'at null' point for doing whatever updates you need. My current approach is a bit messy but you get the idea:
// pending is used to tell the block we're not done, even if result is NULL
BOOL pending = YES;
// resorted is just a flag I use in case there are no videos; if there are none, the block is never called at all, and thus the == NULL part never triggers
__block BOOL resorted = NO;
ALAssetsGroupEnumerationResultsBlock assetEnumerator = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
[assets addObject:result];
} else if (! pending) {
// ready!!
resorted = YES;
[self resort]; // my own method; replace with e.g. tableView reload!
}
};
// there are two types of assets - photos and videos; we start with photo
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
NSLog(@"assets = %d", group.numberOfAssets);
[group enumerateAssetsUsingBlock:assetEnumerator];
// we then set pending to NO; even though the enumeration happens in a separate thread, it seems like pending is not caught by the above enumeration (I have 105 images in the group I am enumerating, FWIW; it may be better to set pending in the == NULL part of the enumeration though
pending = NO;
// now we switch to video and do the same thing
[group setAssetsFilter:[ALAssetsFilter allVideos]];
BriefLog(@"assets = %d", group.numberOfAssets);
[group enumerateAssetsUsingBlock:assetEnumerator];
// if there are 0 vids, the above block is not ever called so we flip to a background thread and then back (probably not necessary) and then check if resorted is set; if it isn't we call resort
if (! resorted) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (! resorted) {
[self resort]; // my own method; replace with e.g. tableView reload!
}
});
});
That's it. The NSRangeExceptions go away. At least in my case they did.