Swift 3 中 AVCaptureSession 到 UIImage 的错误

Erros with AVCaptureSession to UIImage in Swift 3

这是我的设置:

let captureSession = AVCaptureSession()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto

let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

do {
    let input = try AVCaptureDeviceInput(device: backCamera)
    captureSession.addInput(input)
} catch {
    print("can't access camera")
    return
}

let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.view.layer.addSublayer(previewLayer!)

let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample buffer delegate", attributes: .concurrent))

captureSession.addOutput(videoOutput)
captureSession.startRunning()

let pixelBuffer = CMSampleBufferGetImageBuffer(CMSampleBuffer)
let cameraImage = CIImage(cvImageBuffer: pixelBuffer!)


let comicEffect = CIFilter(name: "CIComicEffect")

comicEffect!.setValue(cameraImage, forKey: kCIInputImageKey)

let filteredImage = UIImage(ciImage: comicEffect!.value(forKey: kCIOutputImageKey) as! CIImage!)

DispatchQueue.main.async() {
    let image_view = UIImageView(image: filteredImage)
    self.view = image_view
}

我遇到了两个错误。第一个错误是 setSampleBufferDelegate():

Cannot convert value of type 'ViewController' to expected argument type 'AVCaptureVideoDataOutputSampleBufferDelegate!'

第二个错误是 CMSampleBufferGetImageBuffer():

Cannot convert value of type '(CMSampleBuffer).Type' (aka 'CMSampleBuffer.Type') to expected argument type 'CMSampleBuffer'

有谁知道我哪里做错了吗?

第一个错误:

实施 AVCaptureVideoDataOutputSampleBufferDelegate 委托,这将为您提供当前会话的 AVCaptureOutputCMSampleBuffer

class YourClass : AVCaptureVideoDataOutputSampleBufferDelegate {}

public func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!){ print("DidDropSampleBuffer") //use this CMSampleBuffer to get Image }

对于第二个错误, 替换这个:

let pixelBuffer = CMSampleBufferGetImageBuffer(CMSampleBuffer)

有:

`func ciimageFromSampleBuffer(sampleBuffer : CMSampleBuffer) -> CIImage
  { 
    let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    let cameraImage = CIImage(cvImageBuffer: pixelBuffer!)
    return cameraImage
  }`

这会给你 CIImage.