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

时间:2013-02-11 08:18:54

标签: ios objective-c avfoundation

使用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;

//设置视频预览图层

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

//添加文字图层

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

//甚至添加按钮叠加!!!

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

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

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

1 个答案:

答案 0 :(得分: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