gpt4 book ai didi

ios - 变量显示为 nil - swift 4 IOS

转载 作者:搜寻专家 更新时间:2023-11-01 06:55:33 24 4
gpt4 key购买 nike

出于某种原因,我的变量 stringy 和 stringy 可以很好地打印到控制台,但是当我尝试将它们设置为标签时,它们显示为 nil。

我的目标是将字符串和 float 打印到应用程序 View Controller ,但这是行不通的。

我觉得跟viewdidload有关系,好像隐藏了全局变量。但是,如果我尝试在 viewdidload 之外设置标签,则会出现声明错误。

//  ViewController.swift
// Intellicam
//

import UIKit
import AVKit
import Vision

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

var stringy:String!
var stringie:Float!

override func viewDidLoad() {
super.viewDidLoad()



//here we start the camera

let captureSession = AVCaptureSession()
captureSession.sessionPreset = .photo
guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
guard let input = try? AVCaptureDeviceInput(device: captureDevice) else {return}
captureSession.addInput(input)
captureSession.startRunning()

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

let dataOutput = AVCaptureVideoDataOutput()
dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
captureSession.addOutput(dataOutput)

// let request = VNCoreMLModel(model: VNCoreMLModel, completionHandler: VNRequestCompletionHandler)
// VNImageRequestHandler(cgImage: <#T##CGImage#>, options: <#T##[VNImageOption : Any]#>)

self.Labele.text = "Guess: \(stringy) + Certainty: \(stringie)"

}

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
//print("Camera was able to capture a frame:", Date())

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
//print(finishedReq.results)

guard let results = finishedReq.results as? [VNClassificationObservation] else {return}

guard let firstObservastion = results.first else {return}


//print("Guess: \(firstObservastion.identifier) Certainty: \(firstObservastion.confidence)%")

self.stringy = firstObservastion.identifier
self.stringie = firstObservastion.confidence

print(self.stringy)
print(self.stringie)

}

try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
}

@IBOutlet weak var Labele: UILabel!


}

最佳答案

首先,在您确定值(value)之前,切勿强行解包。在您的情况下, VNCoreModelRequest 可能会失败并且您的 both 变量将被取消分配,因此它会挑衅地使您的应用程序崩溃。还有一件事确保您对标签使用正确的命名约定。

您的问题是您没有根据获得的结果设置标签值。

解决这个问题

 var stringy:String? {
didSet {
DispatchQueue.main.async {
self.Labele.text = self.stringy
}
}
}

        self.stringy = firstObservastion.identifier
self.stringie = firstObservastion.confidence
DispatchQueue.main.async {
self.Labele.text = "Guess: \(stringy) + Certainty: \(stringie)"
}

关于ios - 变量显示为 nil - swift 4 IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53648206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com