简体   繁体   English

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

[英]How to Programmatically Take Photos While Recording Video

The iPhone 5S is capable of taking pictures while recording video and I am trying to figure out how I would do this programatically. iPhone 5S能够在录制视频时拍照,我试图弄清楚如何以编程方式进行此操作。 I know I would be utilizing AVFoundation, however, I couldn't find anything in the programming guide regarding this. 我知道我会使用AVFoundation,但是,我在编程指南中找不到任何关于此的内容。 I have also checked the sample projects (AVFoundation-related) and it doesn't look like there is anything there that does what I am looking for. 我还检查了示例项目 (AVFoundation相关),看起来没有任何东西能够满足我的需求。 if you could help point me in the right direction that would be great. 如果你能帮我指出正确的方向,这将是伟大的。

Actually you can do it with any device that can record video: 实际上你可以用任何可以录制视频的设备来做到这一点:

  • Create and configure a AVCaptureVideoDataOutput . 创建和配置AVCaptureVideoDataOutput
  • Add it to your AVCaptureSession . 将其添加到AVCaptureSession
  • Add a AVCaptureVideoDataOutputSampleBufferDelegate . 添加AVCaptureVideoDataOutputSampleBufferDelegate
  • Implement captureOutput:didOutputSampleBuffer:fromConnection: in the delegate and get the image with imageFromSampleBuffer: . 实现captureOutput:didOutputSampleBuffer:fromConnection:在委托中并使用imageFromSampleBuffer:获取图像imageFromSampleBuffer:

Some similar code can be found here , where images are captured at a given interval, but you only want one image. 这里可以找到一些类似的代码,其中图像以给定的间隔捕获,但您只需要一个图像。

In iOS7 there has a new api to capture UIView, we can get image and do something. 在iOS7中有一个新的api来捕获UIView,我们可以获得图像并做一些事情。

eg: 例如:

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's Document: drawViewHierarchyInRect:afterScreenUpdates: Apple的文档: drawViewHierarchyInRect:afterScreenUpdates:

Through AVCaptureSession you can easily achieve iPhone 5S in the camera app functionality. 通过AVCaptureSession,您可以在相机应用程序功能中轻松实现iPhone 5S。 In your view touch event: 在您的视图触摸事件中:

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

You can get current frame on above delegate method and save it in your gallery. 您可以在上面的委托方法上获取当前帧并将其保存在您的库中。

Please go through below link: 请通过以下链接:

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

I agree with Rivera's answer ! 我同意里维拉的回答! You are going to need AV 你将需要AV

And this is how I do it after the hole AV stuff 这就是我在孔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);

if you want to see the hole sample np. 如果你想看到孔样品np。 I will upload my class 我会上传我的课程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM