ALAssetsLibrary seems to return wrong number of my photos

社会主义新天地 提交于 2019-12-18 06:35:41

问题


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:

"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSOrderedSet enumerateObjectsAtIndexes:options:usingBlock:]: index 14 beyond bounds [0 .. 9]'".'14'

It looks like the number of local photos still remains the same as befoore. And even after I quit my app and restart it again, it still crashes.

Local photo access code:

dispatch_async(dispatch_get_main_queue(), ^
{
   @autoreleasepool 
   {
       ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
       {
           NSLog(@"error occour =%@", [myerror localizedDescription]);
       };

       ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop)
       {
           if (result!=NULL) 
           {
               if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) 
               {
                   [self.g_imageArray addObject:result];
               }
           }
       };

       ALAssetsLibraryGroupsEnumerationResultsBlock
       libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop)
       {
           if (group == nil) 
           {
               return;
           }

           if (group!=nil) {
               [group enumerateAssetsUsingBlock:groupEnumerAtion];
           }
       [self updatephotoList];
       };

       self.library = [[ALAssetsLibrary alloc] init];
       [self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                              usingBlock:libraryGroupsEnumeration 
                            failureBlock:failureblock];
   }
});

If I take another photo with system camera, my application does OK again.


回答1:


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



回答2:


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.




回答3:


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




回答4:


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



回答5:


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.




回答6:


For those of you who fill some NSArray with all the ALAssetRepresentations and don't want to change you code too much, simply filter your array with this Checker - AssetURLChecker

Cheers!




回答7:


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


来源:https://stackoverflow.com/questions/11448965/alassetslibrary-seems-to-return-wrong-number-of-my-photos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!