ios - 在 Swift 中从 AVCaptureSession 捕获静止图像

标签 ios swift avcapturesession

我有一个在 UIView 中显示实时视频的 AVCaptureSession,我想将视频流的一帧保存为 UIImage。我一直在剖析我在互联网上经常看到的代码,但我在第一行遇到了问题:

if let stillOutput = self.stillImageOutput {
    // Establish an AVCaptureConnection and capture a still image from it.
}

这给我错误“相机”没有名为“stillImageOutput”的成员。代码取决于能够从输出中获取视频连接。

如果有帮助,我可以发布完整的代码块。谢谢!

最佳答案

一旦有了stillImageOutput,您就可以使用以下方法来捕获图像

 stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
    if captureSession.canAddOutput(stillImageOutput) {
        captureSession.addOutput(stillImageOutput)
    }

    // I had to add timer otherwise quality was messed up in iPad
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("getImage"), userInfo: nil, repeats: false)

比起我获取图像的函数

func getImage() {

    if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
        stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
            (imageDataSampleBuffer, error) -> Void in
            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)

//          Use your image or store to Album

//          UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData), nil, nil, nil)
            self.stopSession()

        }
    }

}

并停止 session 并删除预览层

func stopSession(){


    self.captureSession.stopRunning()
    self.previewLayer?.removeFromSuperlayer()

}

关于ios - 在 Swift 中从 AVCaptureSession 捕获静止图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26529787/

相关文章:

ios - 使用绘图在 UIScrollView 内缩放 UIView

ios - 同步 UIViewController 转换动画

SwiftUI 在变量更改时调用函数

ios - 如何在iOS中使用Objective C扫描AVCaptureVideoPreviewLayer特定区域的条形码?

ios - 将 Xcode 8 升级到 XCode 9 后出现 AvCapture 错误

ios - 在 Swift 2 iOS 中访问相机

ios - 我无法将按钮连接到@IBAction

ios - Admob 在模拟器中而不是在设备中显示广告

swift 2 : Update TableView from Separate Tab Bar Controller

iphone - 为什么此代码会引发 "CoreData: error: (19) PRIMARY KEY must be unique"错误?