0

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

下面是我的商店照片代码,使用 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是一个字典,其中包含“路径”作为字符串,“大小”作为字符串,“专辑名称”作为数组。

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

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

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

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,我创建的 URL 将不正确,并且此函数将返回带有错误的失败处理程序;

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

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

它永远不会在那条线上崩溃。

有没有什么情况placeholderForCreatedAsset会变成nil?或我未涵盖的任何其他情况可能会导致此问题并生成此崩溃报告?

4

0 回答 0