override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = UIColor.black let image = UIImage(named: "Pic6") let imageView = UIImageView(image: image) imageView.center = CGPoint(x: 185, y: 260)//设置中心点坐标 self.view.addSubview(imageView) let ciImage = CIImage(image: image!) let ciContext: CIContext = { return CIContext(options: nil) }() let ciDetector = CIDetector(ofType: CIDetectorTypeFace, context: ciContext, options: [CIDetectorAccuracy : CIDetectorAccuracyHigh]) let ciImageSize = ciImage!.extent.size var transform = CGAffineTransform.identity transform = transform.scaledBy(x: 1.0, y: -1.0) transform = transform.translatedBy(x: 0, y: ciImageSize.height * -1) let features: [CIFeature]! = ciDetector!.features(in: ciImage!) for feature in features { let frame = feature.bounds.applying(transform) let faceView = UIView(frame: frame) faceView.layer.borderWidth = 2 faceView.layer.borderColor = UIColor.white.cgColor imageView.addSubview(faceView) } }