skip to Main Content

I am actually experimenting with the Vision Framework.
I have simply an UIImageView in my Storyboard and my class is from type UIViewController.
But when I try to override viewDidAppear(_ animated: Bool) I get the error message: Method does not override any method from its superclass
Do anyone know what the issue is? Couldn’t find anything that works for me…

2

Answers


  1. Chosen as BEST ANSWER

    This is my complete code:

    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)
                    
                }
            }
        }
    
        
    }
    

    and:

    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
            }
        }
    }
    

    The first code snipped is from the ViewController file


  2. So after looking at the code, you are declaring a new class called UIViewController. Maybe you meant extension instead of class.

    That’s the reason the compiler could not find the method because you overwrote it by creating that new class by naming it UIViewController.

    You need to replace class UIViewController { with extension UIViewController {

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search