我是Swift的新手,正在关注有关如何为IOS开发图像识别应用程序的教程。我相信我快要完成了,但是我在Xcode中不断收到错误消息,说“调用中缺少参数'completionHandler'的参数。
我已经阅读了有关此错误的其他主题,但不知道如何将提示应用于我自己的代码。我目前正在使用'success:'告诉应用程序是否有成功的标识返回图像分类,但是从我在互联网上阅读的内容来看,应该使用completionHandler来完成。
下面是我的代码,希望能得到一些有关如何解决问题的建议。
import UIKit
import VisualRecognitionV3
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let apiKey = "plxmWfJup7nEkQOwFUTPt9AVMGNd-RYoNK8TLTM_lxq6"
let version = "26-01-19"
@IBOutlet weak var cameraButton: UIBarButtonItem!
@IBOutlet weak var imageView: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
imageView.image = image
imagePicker.dismiss(animated: true, completion: nil)
let visualRecognition = VisualRecognition(version: version, apiKey: apiKey)
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let jpegData = image.jpegData(compressionQuality: 0.01)
let fileURL = documentsURL.appendingPathComponent("tempImage.jpg")
try? jpegData?.write(to: fileURL, options: [])
visualRecognition.classify(imageFile: fileURL, success: { (classifiedImages) -> Void in
print(classifiedImages)
})
}
else{
print("There was an error picking the image")
}
}
@IBAction func cameraTapped(_ sender: UIBarButtonItem) {
imagePicker.sourceType = .savedPhotosAlbum
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
}
我也是堆栈溢出的新手,所以如果我没有正确插入代码,我深表歉意。
谢谢。
编辑:
我使用以下代码解决了这个问题:
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
imageView.image = image
imagePicker.dismiss(animated: true, completion: nil)
let visualRecognition = VisualRecognition(version: version, apiKey: apiKey)
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let imageData = image.jpegData(compressionQuality: 0.5)
let imageURL = documentsURL.appendingPathComponent("tempImage.jpg")
try? imageData?.write(to: imageURL, options: [])
visualRecognition.classify(imagesFile: imageURL) { response, error in
if let error = error {
print(error)
}
guard let classifiedImages = response?.result else {
print("Failed to classify the image")
return
}
let classes = classifiedImages.images.first!.classifiers.first!.classes
for index in 0..<classes.count{
self.classificationResults.append(classes[index].className)
}
print(self.classificationResults)
if self.classificationResults.contains("hotdog"){
DispatchQueue.main.async {
self.navigationItem.title = "Hotdog!"
}
}
else{
DispatchQueue.main.async {
self.navigationItem.title = "Not Hotdog!"
}
}
}
}
}
@IBAction func cameraTapped(_ sender: UIBarButtonItem) {
imagePicker.sourceType = .savedPhotosAlbum
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}