gpt4 book ai didi

ios - 如何在 Swift 4.0 中使用 DispatchQueue

转载 作者:行者123 更新时间:2023-11-28 07:57:54 25 4
gpt4 key购买 nike

我制作了一个使用 CoreML 的图像识别应用程序。从 UI 方面来看,我想在选择图像后返回主视图并在函数 detect() 完成后显示结果。但是,即使我使用 DispatchQueue.main 它也总是同时显示结果和主视图。如果我在 detect() 中使用 DispatchQueue.main,它就可以工作。但我认为它不应该在那里。还有一个问题是 SVProgressHUD.show() 不起作用。

import UIKit
import CoreML
import Vision
import SVProgressHUD

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var cameraButton: UIBarButtonItem!
let imagePicker = UIImagePickerController()

override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self


}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
cameraButton.isEnabled = false
SVProgressHUD.show()



if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{

imageView.image = userPickedImage
imagePicker.dismiss(animated: true, completion: nil)


let userPickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
guard let ciImage = CIImage(image: userPickedImage) else {
fatalError("WRONG")
}
detect(image: ciImage)
}
}

func detect(image: CIImage){

guard let model = try? VNCoreMLModel(for: Inceptionv3().model) else {
fatalError("Loading CoreML Model Failed")
}

let request = VNCoreMLRequest(model: model) { (request, error) in
guard let results = request.results as? [VNClassificationObservation] else{
fatalError("Model failed to process image")
}
if let firstResult = results.first {
let r = firstResult.identifier.split(separator: ",")
DispatchQueue.main.async {
self.cameraButton.isEnabled = true
SVProgressHUD.dismiss()
self.navigationItem.title = r.last?.description
}
}
}


let handler = VNImageRequestHandler(ciImage: image)
do{
try handler.perform([request])
}
catch {
print(error)
}

}

@IBAction func cameraTapped(_ sender: UIBarButtonItem) {
imagePicker.sourceType = .savedPhotosAlbum
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
}

最佳答案

如果我理解正确你的问题,你可以改变如下:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
cameraButton.isEnabled = false
SVProgressHUD.show()

if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
imageView.image = userPickedImage
imagePicker.dismiss(animated: true) {
guard let ciImage = CIImage(image: userPickedImage) else {
fatalError("WRONG")
}
self.detect(image: ciImage)
}
}
}

关于ios - 如何在 Swift 4.0 中使用 DispatchQueue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47491620/

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