CIFeature 没有成员 hasLeftEyePosition
CIFeature has no member hasLeftEyePosition
我正在阅读此 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
,它说:
Value of type 'CIFeature' has no member 'hasLeftEyePosition'
那么如何解决呢
如有任何帮助,我们将不胜感激。谢谢。
我刚刚下载了你提到的项目并且它对我有用(在更新到最新的 Swift 版本之后)。
您遗漏的重要部分在您设置 features
:
的第 9 行
let features = detector.featuresInImage(newImage)
这一定是
let features = detector.featuresInImage(newImage) as! [CIFaceFeature]
我强烈反对强制转换,但由于这不是一个生产应用程序,所以我可以接受。如果你想确保你永远不会崩溃(以防有人将 CIDetectorType
从 "face" 更改为其他内容)你可以保护它并 return 输入图像:
guard let features = detector.featuresInImage(newImage) as? [CIFaceFeature] else { return image }
我正在阅读此 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
,它说:
Value of type 'CIFeature' has no member 'hasLeftEyePosition'
那么如何解决呢
如有任何帮助,我们将不胜感激。谢谢。
我刚刚下载了你提到的项目并且它对我有用(在更新到最新的 Swift 版本之后)。
您遗漏的重要部分在您设置 features
:
let features = detector.featuresInImage(newImage)
这一定是
let features = detector.featuresInImage(newImage) as! [CIFaceFeature]
我强烈反对强制转换,但由于这不是一个生产应用程序,所以我可以接受。如果你想确保你永远不会崩溃(以防有人将 CIDetectorType
从 "face" 更改为其他内容)你可以保护它并 return 输入图像:
guard let features = detector.featuresInImage(newImage) as? [CIFaceFeature] else { return image }