gpt4 book ai didi

swift - CIFeature 没有成员 hasLeftEyePosition

转载 作者:行者123 更新时间:2023-11-30 10:04:13 28 4
gpt4 key购买 nike

我正在阅读此 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”更改为其他内容),您可以保护它并返回输入图像:

guard let features = detector.featuresInImage(newImage) as? [CIFaceFeature] else { return image }

关于swift - CIFeature 没有成员 hasLeftEyePosition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36943682/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com