线程1:EXC错误指令(代码= EXC_1386

时间:2016-10-13 02:58:25

标签: ios swift runtime-error

该错误出现在" guard let faceImage = CIImage。"我已经关注了面部检测的vea软件。问题是什么 ?

override func viewDidLoad() {
    super.viewDidLoad()
    imageView.image = UIImage(named: "FaceDetection") // name of first image to check out
    findFace()
}

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

func findFace() {

    // guard function = if everything equals to nil, then code will break out of function
    guard let faceImage = CIImage(image: imageView.image!) else {return}
    let accuracy = [CIDetectorAccuracy:CIDetectorAccuracyHigh] // highest accuracy for facial recognition
    let faceDector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: accuracy) // searches for faces in still image/video
    let faces = faceDector?.features(in: faceImage, options: [CIDetectorSmile:true, CIDetectorEyeBlink:true])
    for face in faces as! [CIFaceFeature] {
        if face.hasSmile {
            // if face in picture is smiling
            print("☺️")
        }
        if face.leftEyeClosed  {
            // if left eye is closed on the face in the picture
            print("Left:")
        }
        if face.rightEyeClosed {
            // if right eye is closed on the face in the picture
            print("Right:")
        }
    }
        if faces!.count != 0 {
            // face is only detected
        print("Number of Faces: \(faces!.count)")
        } else {
            // no faces detected
            print("No Faces: ")

1 个答案:

答案 0 :(得分:0)

这非常简单。您正在设置imageView.image,如下所示:

imageView.image = UIImage(named: "FaceDetection")

那条线路失败了;你没有这样的形象。因此,imageView.imagenil。然后当你说imageView.image!时强行解开nil。这是Swift的崩溃,所以你崩溃了。