iOS:尝试使用PHPhotoLibrary保存图像时应用崩溃

时间:2016-08-25 15:24:24

标签: ios objective-c iphone crash phphotolibrary

我尝试使用PHPhotoLibrary保存照片。那张照片我作为临时文件保存在app文件夹中。

以下是我的商店照片代码,使用NSOperationQueue

运行背景
- (void)_storePhotoToAblum:(NSDictionary *)photoDic {
    NSString *fileName = (NSString *)[photoDic valueForKey:@"Path"];

    NSString *fileURL = [NSString stringWithFormat:@"%@/%@", _rootPath, fileName];

    NSArray *folders = (NSArray *)[photoDic objectForKey:@"albumName"];

    // Add it to the photo library
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
         PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:[NSURL fileURLWithPath:fileURL]];

        for (NSString *folderID in folders) {
            // if have custom albums
            PHFetchOptions *options = [PHFetchOptions new];
            options.predicate = [NSPredicate predicateWithFormat:@"title = %@", folderID];

            PHAssetCollection *collection = [[PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:options] firstObject];
            if (collection) {
                // Save photo in album
                PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];

                [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];

            }
        }
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
             [self.delegate updateDuplicatePhoto:fileURL withPhotoInfo:photoDic success:YES orError:nil];
        } else {
            NSLog(@"Error:%@", error.localizedDescription);
            [self.delegate updateDuplicatePhoto:fileURL withPhotoInfo:photoDic success:NO orError:error];
        }
    }];
}

photoDic是一个字典,其中包含" path" as String," size" as String和" albumName"作为一个数组。

在开始照片保存过程之前,我已经创建了所有客户相册。

完成处理程序中的委托函数仅用于删除应用程序文档文件夹中的临时文件。

但我在下面收到了一份崩溃报告:

OS Version:          iOS 8.4 (12H143)
Report Version:      105

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Triggered by Thread:  8

Last Exception Backtrace:
0   CoreFoundation                  0x183c642d8 __exceptionPreprocess + 132
1   libobjc.A.dylib                 0x1959380e4 objc_exception_throw + 60
2   CoreFoundation                  0x183b4af70 -[__NSPlaceholderArray initWithObjects:count:] + 412
3   CoreFoundation                  0x183b57200 +[NSArray arrayWithObjects:count:] + 60
4   Content Transf                  0x1001048bc __39-[PhotoStoreHelper _storePhotoToAblum:]_block_invoke + 588 (PhotoStoreHelper.m:109)
5   Photos                          0x187d36424 __51-[PHPhotoLibrary performChanges:completionHandler:]_block_invoke + 48
6   libdispatch.dylib               0x195fb5994 _dispatch_call_block_and_release + 24
7   libdispatch.dylib               0x195fb5954 _dispatch_client_callout + 16
8   libdispatch.dylib               0x195fc00a4 _dispatch_queue_drain + 1448
9   libdispatch.dylib               0x195fb8a5c _dispatch_queue_invoke + 132
10  libdispatch.dylib               0x195fc2318 _dispatch_root_queue_drain + 720
11  libdispatch.dylib               0x195fc3c4c _dispatch_worker_thread3 + 108
12  libsystem_pthread.dylib         0x19619522c _pthread_wqthread + 816
13  libsystem_pthread.dylib         0x196194ef0 start_wqthread + 4

Thread 8 name:
Thread 8 Crashed:
0   libsystem_kernel.dylib          0x00000001960fb270 __pthread_kill + 8
1   libsystem_pthread.dylib         0x000000019619916c pthread_kill + 108
2   libsystem_c.dylib               0x0000000196072b14 abort + 108
3   libc++abi.dylib                 0x0000000195105414 abort_message + 112
4   libc++abi.dylib                 0x0000000195124b88 default_terminate_handler() + 300
5   libobjc.A.dylib                 0x00000001959383bc _objc_terminate() + 124
6   libc++abi.dylib                 0x0000000195121bb0 std::__terminate(void (*)()) + 12
7   libc++abi.dylib                 0x0000000195121474 __cxa_throw + 132
8   libobjc.A.dylib                 0x0000000195938200 objc_exception_throw + 344
9   CoreFoundation                  0x0000000183b4af6c -[__NSPlaceholderArray initWithObjects:count:] + 408
10  CoreFoundation                  0x0000000183b571fc +[NSArray arrayWithObjects:count:] + 56
11  Content Transf                  0x00000001001048b8 __39-[PhotoStoreHelper _storePhotoToAblum:]_block_invoke + 584 (PhotoStoreHelper.m:109)
12  Photos                          0x0000000187d36420 __51-[PHPhotoLibrary performChanges:completionHandler:]_block_invoke + 44
13  libdispatch.dylib               0x0000000195fb5990 _dispatch_call_block_and_release + 20
14  libdispatch.dylib               0x0000000195fb5950 _dispatch_client_callout + 12
15  libdispatch.dylib               0x0000000195fc00a0 _dispatch_queue_drain + 1444
16  libdispatch.dylib               0x0000000195fb8a58 _dispatch_queue_invoke + 128
17  libdispatch.dylib               0x0000000195fc2314 _dispatch_root_queue_drain + 716
18  libdispatch.dylib               0x0000000195fc3c48 _dispatch_worker_thread3 + 104
19  libsystem_pthread.dylib         0x0000000196195228 _pthread_wqthread + 812
20  libsystem_pthread.dylib         0x0000000196194eec start_wqthread + 0

在线坠毁:

[assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];

似乎assetChangeRequest占位符返回nil,我将此nil添加到数组中。

我试图复制此问题,但如果photoDic附带nil,我创建的网址将不正确,此函数将返回错误处理程序错误;

如果fileURL不正确,而我找不到该文件,则此函数将返回失败处理程序,无法完成操作错误而不是崩溃应用程序;

如果获取文件夹信息有问题,则永远不会调用崩溃行,因为我有for循环和if子句来处理空案例。

它永远不会崩溃。

是否有任何案例placeholderForCreatedAsset会变为零?或者我没有覆盖的任何其他情况可能会导致此问题并生成此崩溃报告?

0 个答案:

没有答案