使用叠加文本和图像录制视频

Bis*_*ire 10 objective-c avfoundation ios

使用UIImagePicker控制器或AVFoundation我需要捕获或记录视频,不仅应记录摄像机输出,还应记录覆盖子视图.我已经看到了很多将叠加图像添加到相机预览的示例,但我想要的不仅是叠加图像或文本预览,而是在最终视频输出中实际记录叠加.

//添加图形图层

 CALayer *theDonut = [CALayer layer];
 theDonut.bounds = CGRectMake(50,50, 150, 150);
 theDonut.cornerRadius = 50/2;
 theDonut.backgroundColor = [UIColor clearColor].CGColor;
 theDonut.borderWidth = 50/5;
 theDonut.borderColor = [UIColor orangeColor].CGColor;
Run Code Online (Sandbox Code Playgroud)

//设置视频预览图层

videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:capSession];
videoPreviewLayer.frame = videoPreview.bounds;
videoPreviewLayer.backgroundColor = [UIColor blackColor].CGColor;
videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

videoPreview.layer.masksToBounds = YES;
[videoPreview.layer addSublayer:videoPreviewLayer];
[videoPreview.layer addSublayer:theDonut];
Run Code Online (Sandbox Code Playgroud)

//添加文本图层

UIFont *font = [UIFont fontWithName:@"MarkerFelt-Thin" size:40.0];
    CGSize maxSize = CGSizeMake(480, 10000.0);
    CGSize labelSize = [@"Test Text" sizeWithFont:font constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
    CGRect labelFrame = CGRectMake(50.0, 10.0, labelSize.width, labelSize.height);
    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
    label.font = font;
    label.text = @"Test Text";
    label.numberOfLines = 0;
    [videoPreview addSubview:label];
Run Code Online (Sandbox Code Playgroud)

//甚至添加按钮覆盖!

    UIButton *snap = [UIButton buttonWithType:UIButtonTypeCustom];
    [snap setImage:[UIImage imageNamed:@"takePic"]
          forState:UIControlStateNormal];
    [snap addTarget:self
             action:@selector(pickerCameraSnap:)
   forControlEvents:UIControlEventTouchUpInside];
    snap.frame = CGRectMake(74, 370, 178, 37);

    [videoPreview addSubview:snap];

    // 8) Commit session configuration
    // 9) Start running capture session
    // ========================================
    [capSession commitConfiguration];
    [capSession startRunning];


    //Make sure video is not recording
    [[videoDataOutput connectionWithMediaType:AVMediaTypeVideo] setEnabled:NO];
Run Code Online (Sandbox Code Playgroud)

此示例是从此示例代码修改的 - 它确实将文本和绘图添加到视频预览中,但不会将其记录在最终输出文件中.

http://ioscoreframeworks.com/assets/code/DemoCapture.zip

use*_*101 0

您可以尝试 WWDC 2010 示例代码中包含的 AVEditdemo 应用程序。不幸的是,您需要下载所有 WWDC 2010 示例代码才能获得它 (232.6mb)。您可以在此处获取所有代码的完整下载:http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware ?code=y&source=x&bundleID=20645