1

我正在我的自定义应用程序中填充画廊资产。在我的应用程序中,苹果实时图像被视为视频。我明确写了一个针对苹果实时图像的视频。我的一些苹果实时图像被正确地写为视频,而我的一些图像出现错误。NSError 错误描述说:“操作无法完成。[可可错误-1]。

以下方法对某些媒体对象返回错误:PHAssetResourceManager's writeDataForAssetResource

以下是我的方法,它将苹果实时标识符数组和我的自定义对象数组作为参数,并通过在自定义对象数组中根据传递的苹果实时标识符标识符查找资产来进行处理。

- (void)updateAssetsToVideoFromLivePhotos:(NSMutableArray*)array objectsArray:(NSMutableArray*) mediaObjectsArray withCompletionHandler:(void (^)(void))completionBlock {
    PHFetchOptions *fetchOptions = [PHFetchOptions new];
    PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:array options:fetchOptions];
    
    for (NSInteger i = 0; i < [fetchResult count]; i++) {
        PHAsset *phAsset = [fetchResult objectAtIndex:i];
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        
        NSString *assetID = phAsset.localIdentifier;
        NSRange range = [assetID rangeOfString:@"/"];
        if (range.location != NSNotFound) {
            assetID = [assetID substringToIndex:range.location];
        }
        NSString *outputPath = [documentsDirectory stringByAppendingFormat:@"/%@.mp4", assetID];
        BOOL videoExists = [[NSFileManager defaultManager] fileExistsAtPath:outputPath];
        NSError * errorRef;
        if (videoExists) {
            [[NSFileManager defaultManager] removeItemAtPath:outputPath error:&errorRef];
        }
        NSURL *outputURL = [NSURL fileURLWithPath:outputPath];
        
        NSArray *assetResources = [PHAssetResource assetResourcesForAsset:phAsset];
        for (NSInteger j = 0; j < [assetResources count]; j++){
            PHAssetResource* assetResource = [assetResources objectAtIndex:j];
            if (assetResource.type == PHAssetResourceTypePairedVideo){
                PHAssetResourceRequestOptions *requestOptions =[PHAssetResourceRequestOptions new];
                [[PHAssetResourceManager defaultManager]
                 writeDataForAssetResource:assetResource
                 toFile:outputURL
                 options:requestOptions
                 completionHandler:^(NSError *error) {
                     
                     
                         if (!error){
                             for (NSInteger i = 0; i < [mediaObjectsArray count]; i++){
                                 MyCustomObject *mediaObject = [mediaObjectsArray
                                                               objectAtIndex:i];
                                 if ([mediaObject matchesAssetID:assetID]){
                                     [mediaObject updateForAppleLivePhoto:
                                      [outputURL absoluteString]];
                                     break;
                                 }
                             }
                         } else  {
                             NSLog(@"%@",[error localizedDescription]);
                         }
                 }];
            }
        }
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"mediaUpdated" object:nil];
    });
    completionBlock();
    
}

4

0 回答 0