【问题标题】:Value of type 'CGRect' has no member 'scaled''CGRect' 类型的值没有成员 'scaled'
【发布时间】:2018-04-03 11:31:03
【问题描述】:

我正在尝试在 swift4 中使用 detectLandmarks,但在 let faceBoundingBox = boundingBox.scaled(to: self.view.bounds.size) 出现错误

任何人都知道如何解决它

func detectLandmarks(on image: CIImage) {
    try? faceLandmarksDetectionRequest.perform([faceLandmarks], on: image)
    if let landmarksResults = faceLandmarks.results as? [VNFaceObservation] {
        for observation in landmarksResults {
            DispatchQueue.main.async {
                if let boundingBox = self.faceLandmarks.inputFaceObservations?.first?.boundingBox {
                    let faceBoundingBox = boundingBox.scaled(to: self.view.bounds.size)

                    //different types of landmarks
                    let faceContour = observation.landmarks?.faceContour
                    self.convertPointsForFace(faceContour, faceBoundingBox)    
                }
            }
        }
    }
}

【问题讨论】:

  • 你说有一个错误......但是,你没有告诉我们这个错误是什么。 (将错误放入问题中)。 :D
  • CGRect 确实没有scaled 成员。也许它来自一个框架。

标签: swift cgrect


【解决方案1】:

Try adding in this extension 到你的代码中,事情应该会很好地构建:

//
//  CGRectExtension.swift
//  Vision Face Detection
//
//  Created by Pawel Chmiel on 23.06.2017.
//  Copyright © 2017 Droids On Roids. All rights reserved.
//
import Foundation
import UIKit

extension CGRect {
    func scaled(to size: CGSize) -> CGRect {
        return CGRect(
            x: self.origin.x * size.width,
            y: self.origin.y * size.height,
            width: self.size.width * size.width,
            height: self.size.height * size.height
        )
    }
}

【讨论】:

  • 觉得我会试一试
  • 考虑到它在问题代码中的使用方式,这个实现不太可能是正确的。这个答案似乎假设 size 参数是一个比例因子(可能在 0.1 -> 2.0 范围内)。但问题是传递似乎是视图控制器视图的大小。
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
相关资源
最近更新 更多