5

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

崩溃信息是:

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

看起来本地照片的数量仍然和以前一样。即使在我退出我的应用程序并重新启动它之后,它仍然会崩溃。

本地照片访问代码:

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

如果我用系统相机拍摄另一张照片,我的应用程序会再次正常。

4

7 回答 7

2

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

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];
 };
于 2012-11-28T06:50:07.240 回答
1

注册观察员对我没有帮助。用户仍在崩溃,但我没有。直到今天。

我想出了解决这个崩溃的方法。这最终是苹果照片库中的一个错误,但有一个解决方法。您所做的是,将过滤器设置为照片,然后设置为视频,而不是将其保留在默认的“资产”中。然后,您为每个枚举一次,并做一些技巧以确保您获得最终的“空值”点以进行所需的任何更新。我目前的方法有点混乱,但你明白了:

// 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!
        }
    });
});

就是这样。NSRangeExceptions 消失。至少在我的情况下他们做到了。

于 2012-08-20T16:52:23.327 回答
0

您需要注册一个观察者ALAssetsLibraryChangedNotification以获取库更改。当通知触发时,重新枚举 AssetsLibrary 的组和内容。如果您不注册通知,您的应用程序将获得库的旧快照并且枚举将失败。另请注意,ALAssetsLibraryChangedNotification此处记录的 iOS 5.x 下存在一个错误:http ://www.openradar.me/10484334

于 2012-07-19T07:59:19.690 回答
0

我从秋朗的答案开始,但它对我不起作用。对我有用的是在开始枚举之前使用所有过滤器组合连续调用 setAssetsFilter 3 次。

[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group setAssetsFilter:[ALAssetsFilter allAssets]];
于 2013-02-06T06:32:41.343 回答
0

在 iOS 8 中,我还注意到 numberOfAssets 返回错误数量的照片,如果其中一些被删除并且当前位于“最近删除”相册中。

于 2014-08-26T10:50:33.503 回答
0

对于那些用所有 ALAssetRepresentations 填充一些 NSArray 并且不想过多更改代码的人,只需使用此检查器过滤您的数组 - AssetURLChecker

干杯!

于 2015-02-09T10:27:52.943 回答
0

ALAssetsLibrary 库在 PHAssetsLibrary 上已被贬低,因此请使用以下代码:

__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];
        }
    }];
于 2016-10-28T13:10:01.963 回答