使用AVFoundation录制视频,然后将其上传到parse.com

时间:2014-08-27 12:10:10

标签: ios video parse-platform avfoundation video-recording

我正在使用AVFoundation录制视频,然后允许用户在其上绘图,然后将其上传到parse.com。

我的问题是解析API需要NSDATA将视频上传到服务器,但我不知道如何从AVFoundation制作NSData。这是我的最后一段代码(我主要在视频上应用绘图){

- (void)applyVideoEffectsToComposition:(AVMutableVideoComposition *)composition size:(CGSize)size
{
    UIGraphicsBeginImageContext(_super_view.bounds.size);
    [_super_view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    /// end

    // 1 - set up the overlay
    CALayer *overlayLayer = [CALayer layer];
    UIImage *overlayImage = nil;

    overlayImage =image;

    [overlayLayer setContents:(id)[overlayImage CGImage]];
    overlayLayer.frame = CGRectMake(0, 0, 640,920 );
    [overlayLayer setMasksToBounds:YES];

    // 2 - set up the parent layer
    CALayer *parentLayer = [CALayer layer];
    CALayer *videoLayer = [CALayer layer];
    parentLayer.frame = CGRectMake(0, 0,0,0);
    videoLayer.frame = CGRectMake(0, 0, 640,920);
    [parentLayer addSublayer:videoLayer];
    [parentLayer addSublayer:overlayLayer];

    // 3 - apply magic
    composition.animationTool = [AVVideoCompositionCoreAnimationTool
                                 videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];


}

然后我该怎么做才能将视频上传到服务器?

1 个答案:

答案 0 :(得分:0)

您可以使用AVAssetExportSession执行此操作:

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
 exporter.outputFileType = AVFileTypeQuickTimeMovie;
 exporter.shouldOptimizeForNetworkUse = YES;
 [exporter exportAsynchronouslyWithCompletionHandler:^{
   //Video is saved to file path
    });
}];

url是带有文件路径的NSURL。

将文件保存到文件路径后,您可以使用它来获取它     [NSData initWithContentsOfURL:url]

然后将NSData转换为PFFile,你就可以了!