1

I'am populating gallery assets in my custom app. In my app, apple live images are considered as videos. I explicitly write a video against apple live image. Some of my apple live images are correctly written as videos while i get error for some of my images. NSError error description says: "the operation could not be completed. [cocoa error -1].

Following method returns error for some media objects: PHAssetResourceManager's writeDataForAssetResource

following is my method that takes array of apple live identifiers and my custom object array as parameters and do the processing by finding the assets against passed apple live identifiers identifiers in custom objects array.

- (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();
    
}
Zee
  • 1,865
  • 21
  • 42

0 Answers0