Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

ios - ALAssetsLibrary seems to return wrong number of my photos

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...