将视频从相机胶卷保存到文档

时间:2015-05-21 09:21:13

标签: ios objective-c cocoa-touch video uipickerviewcontroller

如何将从相机胶卷中选择的视频保存到特定目的地?在我的情况下,我想将所选视频保存到destinationPath。另外,我如何才能获得视频的持续时间?

代码:

- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{

    // Video Path
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
    NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]];

    // Save Video

    // Get Video Duration        

    [self dismissViewControllerAnimated:YES completion:nil];

}

1 个答案:

答案 0 :(得分:0)

- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
  {
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
    AVAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
    // Video Path
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
    NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]];
    NSURL *url = [NSURL fileURLWithPath:destinationPath];
    // Save Video
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetHighestQuality];
    exportSession.outputURL = url;
    [exportSession exportAsynchronouslyWithCompletionHandler:nil];
    [binaryImageData writeToFile:destinationPath atomically:YES];
    // Get Video Duration        
    CMTime duration = avAsset.duration;
    [self dismissViewControllerAnimated:YES completion:nil];
  }