UIViewController 无法覆盖其超类中的方法
UIViewController can't override method from it's superclass
我实际上正在试验愿景框架。
我的情节提要中只有一个 UIImageView,我的 class 来自 UIViewController 类型。
但是当我尝试覆盖 viewDidAppear(_ animated: Bool) 时,我收到错误消息:方法不会覆盖其 superclass 中的任何方法
有谁知道问题是什么?找不到适合我的东西...
这是我的完整代码:
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 文件
因此在查看代码后,您声明了一个名为 UIViewController 的新 class。也许您指的是扩展而不是 class。
这就是编译器找不到该方法的原因,因为您通过创建名为 UIViewController 的新 class 覆盖了它。
替换
class UIViewController {
和
extension UIViewController {
我实际上正在试验愿景框架。 我的情节提要中只有一个 UIImageView,我的 class 来自 UIViewController 类型。 但是当我尝试覆盖 viewDidAppear(_ animated: Bool) 时,我收到错误消息:方法不会覆盖其 superclass 中的任何方法 有谁知道问题是什么?找不到适合我的东西...
这是我的完整代码:
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 文件
因此在查看代码后,您声明了一个名为 UIViewController 的新 class。也许您指的是扩展而不是 class。 这就是编译器找不到该方法的原因,因为您通过创建名为 UIViewController 的新 class 覆盖了它。
替换
class UIViewController {
和
extension UIViewController {