没有调用captureOutput,我已经全力以赴寻找解决方案了。 “Hello”字符串不会打印。任何人都可以了解它应该如何运作?我使用的是swift 3,我得到了几乎匹配的可选要求captureOutput(:didDrop:from)
import UIKit
import AVFoundation
class FirstViewController: UIViewController {
@IBOutlet weak var previewView: UIView!
@IBOutlet weak var captureImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Extractor().initCapSession(preView: previewView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func didTakePhoto(_ sender: Any) {
}
}
class Extractor: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
let session = AVCaptureSession()
override init(){
super.init()
}
func initCapSession(preView: UIView) {
session.sessionPreset = AVCaptureSessionPresetHigh
let camera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
let cameraInput = try AVCaptureDeviceInput(device: camera)
let output = AVCaptureVideoDataOutput()
output.alwaysDiscardsLateVideoFrames = true
output.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample buffer"))
session.addInput(cameraInput)
session.addOutput(output)
// preview layer
let preview = AVCaptureVideoPreviewLayer(session: session)
preview?.frame = preView.bounds
preview?.videoGravity = AVLayerVideoGravityResizeAspectFill
preView.layer.insertSublayer(preview!, at: 0)
} catch {
print(error)
}
session.startRunning()
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
print("Hello")
}
}