Couple of ways of dealing with AV file on iOS

filehandler:
http://willtosky.com/2017/01/05/iOS小记/
http://www.cnblogs.com/iOSCain/p/4035359.html

About Sand box
http://www.infocool.net/kb/IOS/201611/215753.html

writeDataForAssetResource
https://stackoverflow.com/questions/33278540/phasset-afnetworking-upload-multiple-videos
http://www.jianshu.com/p/2af1c0365ee4


- (void)assetsPickerController:(GMImagePickerController *)picker didFinishPickingAssets:(NSArray *)assetArray
{
    if(assetArray.count == 0){
        [picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    __weak CDVCamera* weakSelf = self;
    __weak CDVCameraPicker* cameraPicker = (CDVCameraPicker*)picker;
    PHAsset *asset = [assetArray objectAtIndex:0];
    
    dispatch_block_t invoke = ^(void) {
        __block CDVPluginResult* result = nil;
        __block NSString * filePath = nil;
        PHAssetResource *resource = [[PHAssetResource assetResourcesForAsset:asset] firstObject];
        NSLog(@"Filename %@", [resource originalFilename]);
        
        
        if (asset.mediaType == PHAssetMediaTypeImage) {
            PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
            options.version = PHImageRequestOptionsVersionCurrent;
            options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
            options.synchronous = YES;
            
            [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
                // do what you want with it
                NSURL *url = [info objectForKey:@"PHImageFileURLKey"];
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = paths.firstObject;
                NSString *localFile = @"";
                localFile = [localFile stringByAppendingString :[documentsDirectory stringByAppendingPathComponent:[resource originalFilename]]];
                
                [imageData writeToFile:localFile atomically: YES];
                
                result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:localFile];
                
                [weakSelf.commandDelegate sendPluginResult:result callbackId:cameraPicker.callbackId];
                weakSelf.hasPendingOperation = NO;
                weakSelf.pickerController = nil;
            }];
        } else if (asset.mediaType == PHAssetMediaTypeVideo) {
            PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
            options.version = PHImageRequestOptionsVersionCurrent;
            options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
            
            PHImageManager *manager = [PHImageManager defaultManager];
            
            [manager requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {
             
                //resource.originalFilename
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *docPath = paths.firstObject;
                NSString *filename = resource.originalFilename;
                NSString *pathToWrite = [[docPath stringByAppendingPathComponent:filename] stringByAppendingString:@".mp4"];
                exportSession.outputURL = [NSURL fileURLWithPath:pathToWrite];
                exportSession.shouldOptimizeForNetworkUse = NO;
                exportSession.outputFileType = AVFileTypeMPEG4;
                //exportSession.outputFileType = AVFileTypeQuickTimeMovie;
                
                NSError *e1 = [exportSession error];
                [NSFileManager.defaultManager removeItemAtPath: pathToWrite error: &e1];
                [exportSession exportAsynchronouslyWithCompletionHandler:^{
                    switch ([exportSession status]) {
                        case AVAssetExportSessionStatusFailed:
                        {
                            NSError *error = [exportSession error];
                            //NSLog([NSString stringWithFormat:@"Failed to write a resource: %@",[error localizedDescription]]);
                            result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
                        }
                        case AVAssetExportSessionStatusCancelled:
                        {
                            break;
                        }
                        case AVAssetExportSessionStatusCompleted:
                        {
                            result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:pathToWrite];
                            [weakSelf.commandDelegate sendPluginResult:result callbackId:cameraPicker.callbackId];
                        }
                        
                        default:
                            break;
                    }
                
                }];
                
            }];
             
            
            
            /*
            __block PHVideoRequestOptions *videoRequestOptions = [[PHVideoRequestOptions alloc] init];
            videoRequestOptions.version = PHVideoRequestOptionsVersionOriginal;
            videoRequestOptions.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat;
            
            
            
            [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:videoRequestOptions resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
                 if ([asset isKindOfClass:[AVURLAsset class]]) {
                     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                     NSString *docPath = [paths objectAtIndex:0];
                     NSString *filename = resource.originalFilename;
                     NSString *pathToWrite = [docPath stringByAppendingPathComponent:filename];
                     NSURL *dest = [NSURL fileURLWithPath:pathToWrite];
                     
                     NSError *err = nil;
                     if([NSFileManager.defaultManager fileExistsAtPath:dest.absoluteString]){
                         [NSFileManager.defaultManager removeItemAtPath:dest.absoluteString error:&err];
                     }
                     
                     PHAssetResourceRequestOptions *options = [PHAssetResourceRequestOptions new];
                     [[PHAssetResourceManager defaultManager] writeDataForAssetResource:resource toFile:dest options: options completionHandler:^(NSError * _Nullable error) {
                         if (error) {
                             NSLog([NSString stringWithFormat:@"Failed to write a resource: %@",[error localizedDescription]]);
                             result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
                         } else {
                             result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[dest absoluteString]];
                             [weakSelf.commandDelegate sendPluginResult:result callbackId:cameraPicker.callbackId];
                         }
                     }];
                 }
             }];
             */
        }
        
    };
    
    [picker.presentingViewController dismissViewControllerAnimated:YES completion:invoke];
    
    NSLog(@"Length: %lu", (unsigned long)assetArray.count);
}

评论

此博客中的热门博文

JavaScript quiz - Scoping related

Java SMTP

qtCreator + Eclipse