本文介绍了AVCaptureVideo不显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从控制台保留对象,以将其显示为标签(classifierText).出现警告"UILabel.text必须仅从主线程使用".为什么将这些商品显示为标签,这似乎有什么问题?
I am trying to retain the objects from the console to be shown in as a label(classifierText). The warning of "UILabel.text must be used from main thread only" appears. What seems to the problem as to why the items are being shown as the label?
var previewLayer: AVCaptureVideoPreviewLayer!
let classifierText: UILabel = {
let classifier = UILabel()
classifier.translatesAutoresizingMaskIntoConstraints = false
classifier.textColor = .black
classifier.font = UIFont(name: "Times-New-Roman", size: 10)
classifier.textAlignment = .center
return classifier
}()
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
guard let model = try? VNCoreMLModel(for: Resnet50().model) else { return }
let request = VNCoreMLRequest(model: model) { (finishedReq, err) in
let results = finishedReq.results as? [VNClassificationObservation]
let firstObservation = results!.first
self.classifierText.text = "\(firstObservation!.identifier as String)"
推荐答案
方法captureOutput(sampleBuffer, etc)
不在主线程上运行.因此,您正在尝试从不是主线程的线程更改标签.
The method captureOutput(sampleBuffer, etc)
does not run on the main thread. So you're trying to change the label from a thread that is not the main thread.
解决方案是将工作安排在主线程上,如下所示:
The solution is to schedule the work on the main thread, like so:
DispatchQueue.main.async {
self.classifierText.text = "\(firstObservation!.identifier as String)"
}
这篇关于AVCaptureVideo不显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!