2016-11-29 75 views
-1

我在製作人臉檢測應用程序。我寫了一些代碼來檢測,但我收到一個錯誤。請建議我一個替代解決方案。 下面是代碼,錯誤是行號3錯誤:「使用未解析的標識符」

import UIKit 
import CoreImage 

class ViewController: UIViewController ,UIAlertViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    @IBOutlet var imageView: UIImageView! 
    @IBAction func Moodify(_ sender: UIButton) { 

     let picker = UIImagePickerController() 
     picker.delegate = self 
     picker.allowsEditing = true 
     picker.sourceType = .camera 
     self.present(picker, animated: true, completion: { _ in }) 


     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [AnyHashable: Any]) { 
      let chosenImage = info[UIImagePickerControllerEditedImage] 
      self.imageView!.image = chosenImage as? UIImage 
      picker.dismiss(animated: true, completion: { _ in }) 
     } 

     func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
      picker.dismiss(animated: true, completion: { _ in }) 
     } 

    } 

    func detect() { 

     guard let personciImage = CIImage(image: personPic.image!) else { 
      return 
     } 

     let accuracy = [CIDetectorAccuracy: CIDetectorAccuracyHigh] 
     let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: accuracy) 
     let faces = faceDetector.featuresInImage(personciImage) 

     for face in faces as! [CIFaceFeature] { 

      print("Found bounds are \(face.bounds)") 

      let faceBox = UIView(frame: face.bounds) 

      faceBox.layer.borderWidth = 3 
      faceBox.layer.borderColor = UIColor.redColor().CGColor 
      faceBox.backgroundColor = UIColor.clearColor() 
      personPic.addSubview(faceBox) 

      if face.hasLeftEyePosition { 
       print("Left eye bounds are \(face.leftEyePosition)") 
      } 

      if face.hasRightEyePosition { 
       print("Right eye bounds are \(face.rightEyePosition)") 
      } 
     } 
    }  

    override func viewDidLoad() { 

     let alert = UIAlertController(title: "Error", message: "Device has no camera", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil)) 
     self.present(alert, animated: true, completion: nil) 


    /* if !UIImagePickerController.isSourceTypeAvailable(.camera) { 
      let myAlertView = UIAlertView (title: "Error", message: "Device has no camera", delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "") 
      myAlertView.show() 
     } */ 

     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

SEE ERROR IN THIS IMAGE

+0

哪裏定義了'personPic'?從哪裏來? – Larme

+0

「personPic」在哪裏申報? –

+0

另外你爲什麼拒絕我的編輯?它修復了你的問題中的格式錯誤。目前您的圖片不可訪問。 –

回答

1

您需要聲明,並連接到故事板一個UIImageView。

@IBOutlet weak var personPic: UIImageView! 

這是基本的編程。你怎麼能使用你沒有聲明的東西,對吧?祝你好運在你的項目。

+1

非常感謝您親愛的,我用我的imageView替換了personPic,我將其作爲IBOutlet連接。現在它不顯示任何錯誤。 –