我正在阅读https://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1中的GCD
教程。
当我下载已启动的项目并构建时,我收到了一些错误:
private extension PhotoDetailViewController {
func faceOverlayImageFromImage(image: UIImage) -> UIImage {
let detector = CIDetector(ofType: CIDetectorTypeFace,
context: nil,
options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
// Get features from the image
let newImage = CIImage(CGImage: image.CGImage!)
let features = detector.featuresInImage(newImage)
UIGraphicsBeginImageContext(image.size)
let imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
// Draws this in the upper left coordinate system
image.drawInRect(imageRect, blendMode: CGBlendMode.Normal, alpha: 1.0)
let context = UIGraphicsGetCurrentContext()
for faceFeature in features {
let faceRect = faceFeature.bounds
CGContextSaveGState(context)
// CI and CG work in different coordinate systems, we should translate to
// the correct one so we don't get mixed up when calculating the face position.
CGContextTranslateCTM(context, 0.0, imageRect.size.height)
CGContextScaleCTM(context, 1.0, -1.0)
if faceFeature.hasLeftEyePosition {
let leftEyePosition = faceFeature.leftEyePosition
let eyeWidth = faceRect.size.width / FaceBoundsToEyeScaleFactor
let eyeHeight = faceRect.size.height / FaceBoundsToEyeScaleFactor
let eyeRect = CGRect(x: leftEyePosition.x - eyeWidth / 2.0,
y: leftEyePosition.y - eyeHeight / 2.0,
width: eyeWidth,
height: eyeHeight)
drawEyeBallForFrame(eyeRect)
}
if faceFeature.hasRightEyePosition {
let leftEyePosition = faceFeature.rightEyePosition
let eyeWidth = faceRect.size.width / FaceBoundsToEyeScaleFactor
let eyeHeight = faceRect.size.height / FaceBoundsToEyeScaleFactor
let eyeRect = CGRect(x: leftEyePosition.x - eyeWidth / 2.0,
y: leftEyePosition.y - eyeHeight / 2.0,
width: eyeWidth,
height: eyeHeight)
drawEyeBallForFrame(eyeRect)
}
CGContextRestoreGState(context);
}
let overlayImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return overlayImage
}
在这一行faceFeature.hasLeftEyePosition
,它说:
'CIFeature'类型的值没有成员'hasLeftEyePosition'
那么如何解决这个问题。
任何帮助将不胜感激。感谢。
答案 0 :(得分:1)
我刚刚下载了您提到的项目,它正在为我工作(在将其更新为最新的Swift版本之后)。
您缺少的重要内容是在您设置features
的第9行:
let features = detector.featuresInImage(newImage)
这一定是
let features = detector.featuresInImage(newImage) as! [CIFaceFeature]
我强烈反对强制投射,但由于这不是一个制作应用程序,我可以使用它。如果你想确保永远不会崩溃(如果有人将CIDetectorType
从“脸部”更改为其他内容)你可以保护它并返回输入图像:
guard let features = detector.featuresInImage(newImage) as? [CIFaceFeature] else { return image }