我实际上是在尝试Vision Framework。我在Storyboard中有一个简单的UIImageView,我的类来自类型UIViewController。但是当我试图覆盖viewDidAppear(_ animated:返回错误消息:方法不会覆盖它的超类中的任何方法有人知道问题出在哪里吗?找不到任何适合我的方法...
l7wslrjt1#
看完代码后,你声明了一个名为UIViewController的新类,也许你指的是扩展而不是类。这就是编译器找不到该方法的原因,因为您通过将其命名为UIViewController来创建新类,从而覆盖了该方法。您需要将class UIViewController {替换为extension UIViewController {
UIViewController
class UIViewController {
extension UIViewController {
sqserrrh2#
这是我的完整代码:
import UIKit import Vision class ViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! var imageOrientation = CGImagePropertyOrientation(.up) override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if let image = UIImage(named: "group") { imageView.image = image imageView.contentMode = .scaleAspectFit imageOrientation = CGImagePropertyOrientation(image.imageOrientation) guard let cgImage = image.cgImage else {return} setupVision(image: cgImage) } } private func setupVision (image: CGImage) { let faceDetectionRequest = VNDetectFaceRectanglesRequest(completionHandler: self.handelFaceDetectionRequest) let imageRequestHandler = VNImageRequestHandler(cgImage: image, orientation: imageOrientation, options: [:]) do { try imageRequestHandler.perform([faceDetectionRequest]) }catch let error as NSError { print(error) return } } private func handelFaceDetectionRequest (request: VNRequest?, error: Error?) { if let requestError = error as NSError? { print(requestError) return } guard let image = imageView.image else {return} guard let cgImage = image.cgImage else {return} let imageRect = self.determineScale(cgImage: cgImage, imageViewFrame: imageView.frame) self.imageView.layer.sublayers = nil if let results = request?.results as? [VNFaceObservation] { for observation in results { let faceRect = convertUnitToPoint(originalImageRect: imageRect, targetRect: observation.boundingBox) let emojiRect = CGRect(x: faceRect.origin.x, y: faceRect.origin.y - 5, width: faceRect.size.width + 5, height: faceRect.size.height + 5) let textLayer = CATextLayer() textLayer.string = "🦸♂️" textLayer.fontSize = faceRect.width textLayer.frame = emojiRect textLayer.contentsScale = UIScreen.main.scale self.imageView.layer.addSublayer(textLayer) } } } }
以及:
import UIKit class UIViewController { public func convertUnitToPoint (originalImageRect: CGRect, targetRect: CGRect) -> CGRect { var pointRect = targetRect pointRect.origin.x = originalImageRect.origin.x + (targetRect.origin.x * originalImageRect.size.width) pointRect.origin.y = originalImageRect.origin.y + (1 - targetRect.origin.y - targetRect.height) pointRect.size.width *= originalImageRect.size.width pointRect.size.height *= originalImageRect.size.height return pointRect } public func determineScale (cgImage: CGImage, imageViewFrame: CGRect) -> CGRect { let originalWidth = CGFloat(cgImage.width) let originalHeigth = CGFloat(cgImage.height) let imageFrame = imageViewFrame let widthRatio = originalWidth / imageFrame.width let heigthRatio = originalHeigth / imageFrame.height let scaleRatio = max(widthRatio, heigthRatio) let scaledImageWidth = originalWidth / scaleRatio let scaledImageHeigth = originalHeigth / scaleRatio let scaledImageX = (imageFrame.width - scaledImageWidth) / 2 let scaledImageY = (imageFrame.height - scaledImageHeigth) / 2 return CGRect(x: scaledImageX, y: scaledImageY, width: scaledImageWidth, height: scaledImageHeigth) } } extension CGImagePropertyOrientation { init(_ orientation: UIImage.Orientation) { switch orientation { case .up: self = .up case .upMirrored: self = .upMirrored case .down: self = .down case .downMirrored: self = .downMirrored case .right: self = .right case .rightMirrored: self = .rightMirrored default: self = .up } } }
剪切的第一个代码来自ViewController文件
2条答案
按热度按时间l7wslrjt1#
看完代码后,你声明了一个名为
UIViewController
的新类,也许你指的是扩展而不是类。这就是编译器找不到该方法的原因,因为您通过将其命名为
UIViewController
来创建新类,从而覆盖了该方法。您需要将
class UIViewController {
替换为extension UIViewController {
sqserrrh2#
这是我的完整代码:
以及:
剪切的第一个代码来自ViewController文件