【问题标题】:How to Programmatically Take Photos While Recording Video如何在录制视频时以编程方式拍照
【发布时间】:2014-05-20 15:16:33
【问题描述】:

iPhone 5S 能够在录制视频的同时拍照,我正试图弄清楚如何以编程方式做到这一点。我知道我会使用 AVFoundation,但是,我在 programming guide 中找不到任何关于此的内容。我还检查了sample projects(与 AVFoundation 相关的),看起来那里没有任何东西可以满足我的要求。如果您能帮助我指出正确的方向,那就太好了。

【问题讨论】:

  • 表示您想从视频中捕获照片(如视频封面图片)??
  • 不,谢谢你的提问。在 iPhone 5S 的相机应用程序中,在进行视频录制时,您可以拍照(通过点击侧面的白色按钮)。我想以编程方式在我的应用程序中实现这种功能。我什至会考虑私有 API。

标签: iphone objective-c ios7 avfoundation


【解决方案1】:

实际上,您可以使用任何可以录制视频的设备来做到这一点:

  • 创建和配置AVCaptureVideoDataOutput
  • 将其添加到您的AVCaptureSession
  • 添加AVCaptureVideoDataOutputSampleBufferDelegate
  • 在委托中实现captureOutput:didOutputSampleBuffer:fromConnection:,并通过imageFromSampleBuffer:获取图像。

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

【讨论】:

  • 我们不能在录制同一视频的同时使用这个过程来获取视频片段吗?
【解决方案2】:

在 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:

【讨论】:

  • 哇!这种新方法确实适用于视频,不像旧方法 [CALayer renderInContext:]
【解决方案3】:

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

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

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

请通过以下链接:

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

【讨论】:

    【解决方案4】:

    我同意里维拉的回答!你将需要 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.我会上传我的课程

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      相关资源
      最近更新 更多