【问题标题】:PHAssetCollection fetchAssetCollectionsWithType crashesPHAssetCollection fetchAssetCollectionsWithType 崩溃
【发布时间】:2017-10-19 20:08:05
【问题描述】:

为了从设备获取几张照片,将以下方法放在默认全局队列中。如果系统版本大于 iOS 8,则使用Photos 框架,否则使用ALAssetLibrary

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

dispatch_async(dispatch_get_global_queue(0, 0), ^{
   [self getPhotos:^(NSArray *photos) {
       dispatch_semaphore_signal(sema);
    }];
}

dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC));

dispatch_semaphore_wait(sema, timeout);


- (void)getPhotos:(MyCallBack)callback {

  Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary");

   if (PHPhotoLibrary_class) {

    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];


    if (status != PHAuthorizationStatusAuthorized)
    {
        return;
    }

    PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];

  }       
}

应用程序崩溃,在64位设备上,例如iPhone 5S和更新型号,堆栈跟踪是这样的。

Exception Type:  SIGTRAP
Exception Codes: TRAP_BRKPT at 0x190c41b60
Crashed Thread:  25

Thread 25 Crashed:
0   PhotoLibraryServices            0x0000000190c41b60 +[PLModelMigrator _validateCurrentModelVersionFailedWithNoVersionFromServer] :444 (in PhotoLibraryServices)
1   PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
2   PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
3   PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
4   PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
5   PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
6   PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
7   PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
8   PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
9   PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
10  PhotoLibraryServices            0x0000000190c41ef8 +[PLModelMigrator _validateCurrentModelVersionAttempt:] :316 (in PhotoLibraryServices)
11  PhotoLibraryServices            0x0000000190c43688 +[PLModelMigrator createDatabase] :508 (in PhotoLibraryServices)
12  PhotoLibraryServices            0x0000000190b7c1cc -[PLPhotoLibrary loadDatabase:] :808 (in PhotoLibraryServices)
13  PhotoLibraryServices            0x0000000190b71f70 -[PLPhotoLibrary initWithTransientContext:name:] :560 (in PhotoLibraryServices)
14  Photos                          0x0000000191722418 ___45-[PHPhotoLibrary backgroundQueuePhotoLibrary]_block_invoke :60 (in Photos)
15  libdispatch.dylib               0x0000000184bb21bc __dispatch_client_callout :16 (in libdispatch.dylib)
16  libdispatch.dylib               0x0000000184bb2fb0 _dispatch_once_f :56 (in libdispatch.dylib)
17  Photos                          0x00000001917223d4 -[PHPhotoLibrary backgroundQueuePhotoLibrary] :156 (in Photos)
18  Photos                          0x0000000191722604 -[PHPhotoLibrary photoLibraryForCurrentQueueQoS] :80 (in Photos)
19  Photos                          0x0000000191722578 -[PHPhotoLibrary managedObjectContextForCurrentQueueQoS] :24 (in Photos)
20  Photos                          0x00000001917611bc -[PHQuery _createFetchRequestIncludingBasePredicate:] :140 (in Photos)
21  Photos                          0x0000000191761c5c -[PHQuery fetchRequest] :52 (in Photos)
22  Photos                          0x0000000191783988 -[PHFetchResult initWithQuery:oids:registerIfNeeded:usingManagedObjectContext:] :376 (in Photos)
23  Photos                          0x0000000191783c2c -[PHFetchResult initWithQuery:] :100 (in Photos)
24  Photos                          0x0000000191763b5c -[PHQuery executeQuery] :52 (in Photos)
25  Photos                          0x00000001916c2998 ___67+[PHAssetCollection fetchAssetCollectionsWithType:subtype:options:]_block_invoke :72 (in Photos)
26  Photos                          0x000000019175fe48 +[PHObject authorizationAwareFetchResultWithOptions:fetchBlock:] :88 (in Photos)
27  Photos                          0x00000001916c2944 +[PHAssetCollection fetchAssetCollectionsWithType:subtype:options:] :84 (in Photos)

通常在32位设备上,如iPhone5、iPhone4S,crash报告是这样的:

Exception Type:  SIGABRT
Exception Codes: #0 at 0x1b448ad4
Crashed Thread:  1
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException'  reason: 'CoreData: Cannot load NSManagedObjectModel.  nil is an illegal URL parameter'
Last Exception Backtrace:
0   CoreFoundation                  0x1bca0df7 ___exceptionPreprocess :131 (in CoreFoundation)
1   libobjc.A.dylib                 0x1af03077 _objc_exception_throw :39 (in libobjc.A.dylib)
2   CoreData                        0x1dc190c5 +[NSManagedObjectContext initialize] :1 (in CoreData)
3   PhotoLibraryServices            0x256a45d5 ___44+[PLManagedObjectContext managedObjectModel]_block_invoke :89 (in PhotoLibraryServices)
4   AssetsLibraryServices           0x24fc527d ___pl_dispatch_once_block_invoke :19 (in AssetsLibraryServices)
5   libdispatch.dylib               0x1b346083 __dispatch_client_callout :23 (in libdispatch.dylib)
6   libdispatch.dylib               0x1b346c65 _dispatch_once_f :43 (in libdispatch.dylib)
7   AssetsLibraryServices           0x24fc5267 _pl_dispatch_once :77 (in AssetsLibraryServices)
8   PhotoLibraryServices            0x256a456d +[PLManagedObjectContext managedObjectModel] :73 (in PhotoLibraryServices)
9   Photos                          0x26139475 +[PHQuery _relationshipForFetchType:predicate:] :257 (in Photos)
10  Photos                          0x2613a4cd -[PHQuery collectionFetchType] :85 (in Photos)
11  Photos                          0x261392b1 -[PHQuery copyWithZone:] :303 (in Photos)
12  Photos                          0x26158239 -[PHFetchResult initWithQuery:oids:registerIfNeeded:usingManagedObjectContext:] :125 (in Photos)
13  Photos                          0x26158567 -[PHFetchResult initWithQuery:] :81 (in Photos)
14  Photos                          0x2613c0cb -[PHQuery executeQuery] :53 (in Photos)
15  Photos                          0x260aaf4f ___67+[PHAssetCollection fetchAssetCollectionsWithType:subtype:options:]_block_invoke :73 (in Photos)
16  Photos                          0x2613898d +[PHObject authorizationAwareFetchResultWithOptions:fetchBlock:] :63 (in Photos)
17  Photos                          0x260aaf03 +[PHAssetCollection fetchAssetCollectionsWithType:subtype:options:] :97 (in Photos)

这似乎是一个coredata错误,我也不明白为什么AssetsLibraryServicesPhotoLibraryServices都存在于上次崩溃报告中。我尝试了很多方法,但无法重现崩溃,感谢任何帮助。

【问题讨论】:

  • 您的 plist 是否包含使用说明? (developer.apple.com/library/content/documentation/General/…)
  • @danh,是的,NSPhotoLibraryUsageDescription 包括在内。
  • 至少从32bit崩溃来看,似乎是个例外。添加异常断点,在调试提示符下输入po $arg1
  • 您在应用程序生命周期的哪个阶段运行此代码?
  • @Aris,应用启动,我创建了一个NSOperationQueue 和一个NSOperation,并将操作添加到队列中。

标签: ios objective-c photosframework


【解决方案1】:

这里缺少几件事,主要是您致电getPhotos 的上下文,我注意到您从未对smartAlbums 进行任何操作或回电。也许您只是没有包含它,因为您认为它不相关。

无论如何,我创建了一个新项目(包括核心数据),这就是我所做的一切。无崩溃工作:

1) 将NSPhotoLibraryUsageDescription 添加到 plist

2) 将以下内容添加到AppDelegate.h

#import <Photos/Photos.h>

typedef void (^MyCallBack)(NSArray *photos);

3) 将AppDelegate.mdidFinishLaunchingWithOptions 的默认实现替换为:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

        if (status == PHAuthorizationStatusAuthorized) {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                [self getPhotos:^(NSArray *photos) {
                    dispatch_semaphore_signal(sema);
                }];
            });
        }
    }];

    dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC));

    dispatch_semaphore_wait(sema, timeout);

    return YES;
}

- (void)getPhotos:(MyCallBack)callback {

    Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary");

    if (PHPhotoLibrary_class) {

        PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];

        NSMutableArray *photos = [[NSMutableArray alloc] init];

        int i = 0;
        for (PHAssetCollection *assetCollection in smartAlbums)
        {
            i++;

            PHFetchResult *smartAlbum = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];

            int j = 0;
            for (PHAsset *asset in smartAlbum)
            {
                j++;

                [[PHImageManager defaultManager]
                 requestImageDataForAsset:asset
                 options:nil
                 resultHandler:^void(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {

                     [photos addObject:[UIImage imageWithData:imageData]];
                     if (i == [smartAlbums count] && j == [smartAlbum count]) {
                         callback([NSArray arrayWithArray:photos]);
                     }
                 }];

                if (i == [smartAlbums count] && j == [smartAlbum count]) {
                    return;
                }
            }
        }
    }
}

【讨论】:

  • 我对@9​​87654329@ 做了类似的事情,从崩溃报告来看,崩溃发生在fetchAssetsInAssetCollection 行,之后的代码似乎没有被执行。所以我省略了,当它崩溃时有 30 个线程在运行。
  • 正如您在其他地方所说的,这可能与 CoreData 而不是资产获取有关。我们已经确定我的代码是在真空中工作的,这实际上只是您提供的经过一些修改的代码。如果您删除/注释掉对 getPhotos 的调用(当然还有相关的 dispatch_semaphore_wait),您还会崩溃吗?
  • 这可能是Photos Framework是建立在CoreData之上的,用户拍摄的图片保存在系统数据库中,有多个任务同时运行,而且我不是唯一可以访问Photos框架的人,它可能是数据库被另一个线程获取了,它没有响应我的请求。该应用在应用商店中,我无法重现崩溃,但某些用户可以。
  • 啊,我明白了。是的,您的首要任务应该是复制,如果您不首先这样做,就永远无法真正修复错误
  • 您是否设法重现此问题?这是一个无法解决的问题的大赏金!
【解决方案2】:

根据我的经验,子线程中没有必要getPhotosfetchAssetCollectionsWithType。试试看吧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 2021-05-14
    • 1970-01-01
    • 2016-11-09
    • 2018-12-11
    相关资源
    最近更新 更多