Search code examples
iosvideoparse-platformavfoundationvideo-recording

Using AVFoundation to record video then upload it to parse.com


I am using AVFoundation to record video then allow user to draw on it, then upload it to parse.com.

My issue is parse API needs NSDATA to upload video to server, but I don't know how I can make NSData from AVFoundation. This my last code (I main apply draw on video ) {

- (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];


}

Then what can i do to upload video to server?


Solution

  • You can use the AVAssetExportSession to do this:

    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 is an NSURL with a file path.

    Once the file is saved to the file path you can fetch it using [NSData initWithContentsOfURL:url]

    Then convert the NSData into a PFFile and you are good to go!