如何在录制视频时以编程方式拍摄照片

时间:2014-04-08 05:35:39

标签: iphone objective-c ios7 avfoundation

iPhone 5S能够在录制视频时拍照,我正在试图弄清楚如何以编程方式执行此操作。我知道我会使用AVFoundation,但是,我在programming guide中找不到任何关于此的内容。我还检查了sample projects(AVFoundation相关),看起来没有任何东西可以满足我的需求。如果你能帮助我指出一个很好的方向。

4 个答案:

答案 0 :(得分:4)

实际上,您可以使用任何可以录制视频的设备来执行此操作:

  • 创建并配置AVCaptureVideoDataOutput
  • 将其添加到您的AVCaptureSession
  • 添加AVCaptureVideoDataOutputSampleBufferDelegate
  • 在委托中实施captureOutput:didOutputSampleBuffer:fromConnection:并使用imageFromSampleBuffer:获取图片。

可以找到一些类似的代码here,其中图像以给定的间隔捕获,但您只需要一个图像。

答案 1 :(得分:3)

在iOS7中有一个新的api来捕获UIView,我们可以获取图像并做一些事情。

例如:

UIView+Screenshot.h
-(UIImage *)convertViewToImage;

UIView+Screenshot.m

-(UIImage *)convertViewToImage
{
    UIGraphicsBeginImageContext(self.bounds.size);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

Apple的文档:drawViewHierarchyInRect:afterScreenUpdates:

答案 2 :(得分:1)

通过AVCaptureSession,您可以在相机应用程序功能中轻松实现iPhone 5S。 在您的视图触摸事件中:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection "

您可以在上面的委托方法中获取当前帧并将其保存在您的图库中。

请通过以下链接:

https://developer.apple.com/library/ios/samplecode/RosyWriter/Introduction/Intro.html

答案 3 :(得分:0)

我同意里维拉的回答!你将需要AV

这就是我在孔AV之后做的事情

CGImageRef bigImage = image.CGImage;
CGRect rectImage = CGRectMake(self.scannArea.origin.x*widhtRatio,
                              self.scannArea.origin.y*heightRatio - 50,
                              self.scannArea.size.width*widhtRatio,
                              self.scannArea.size.height*heightRatio);
CGImageRef part = CGImageCreateWithImageInRect(bigImage, rectImage);
UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:part], nil, nil, nil);

image = [self drawImage:[UIImage imageNamed:@"overlaygraphic.png"] 
                inImage:image 
                atPoint:CGPointMake(self.scannArea.origin.x*widhtRatio, 
                                    self.scannArea.origin.y*heightRatio - 50)];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

如果你想看到孔样品np。我会上传我的课程

相关问题