ALAssetsLibrary似乎返回错误的照片数量 [英] ALAssetsLibrary seems to return wrong number of my photos

查看:163
本文介绍了ALAssetsLibrary似乎返回错误的照片数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用ALAssetsLibrary获取本地照片时,它可以正常工作。但是在我使用照片应用程序删除一些照片后,我的应用程序崩溃了。

When I use ALAssetsLibrary to get local photos it works fine. But after I delete some photos with the 'Photos' application my app crashes.

崩溃信息是:

因未捕获的异常而终止应用程序'NSRangeException',原因:'*** - [NSOrderedSet enumerateObjectsAtIndexes:options:usingBlock:]:索引14超出边界[0 .. 9]'。'14'

看起来本地照片的数量仍然与befoore相同。即使在我退出应用程序并重新启动它之后,它仍然会崩溃。

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.

本地照片访问代码:

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.

推荐答案

这似乎是一个iOS错误,就像你说ALAssetsLibrary返回错误的照片数量所以你得到了索引越界错误。解决方法是再次重新加载您的照片,如下所示:

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

这篇关于ALAssetsLibrary似乎返回错误的照片数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆