我試圖讓用戶在單擊按鈕操作時拍照。我已將 UIImageView 添加到我的視圖控制器的故事板中。我已將此控制元件的名稱設定為 imageView,但是當我實作 imageView.image = image 時,出現錯誤“在范圍內找不到 'imageView'”。我還嘗試將其設定為 self.imageView = image,但出現錯誤“無法在范圍內找到自我”。我不確定為什么我的連接到 @IBOutlet 的 imageView 沒有被找到?
import UIKit
class HomeScreenViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
var button: UIButton! {
imageView.backgroundColor = .secondarySystemBackground
self.button.backgroundColor = .systemBlue
}
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func didTapButton(){
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.delegate = self
present(picker, animated: true)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
return
}
imageView.image = image
}
uj5u.com熱心網友回復:
你的函式不在你的類中!!您應該將函式放在類中,如下所示:
class HomeScreenViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]){
// any code
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361694.html
