'Value of type UIImage has no member cgImageOrientation

I am new coreml and I was reading a book I came across a code snippet and when I tried to run it I got the above error , I Googled but got no luck , here is my code

import UIKit
import Vision
extension UIImage {
    func detectFaces(completion:@escaping ([VNFaceObservation]?)->()){
        guard let image = self.cgImage else {
            completion(nil)
            return
        }
        let request = VNDetectFaceRectanglesRequest()
        DispatchQueue.global().async {
            let handler = VNImageRequestHandler(cgImage: image, orientation: self.cgImageOrientation)
            //above error here I think cgImageOrientation is no longer available but what's the solution anyways
        }
    }
}

here I am using swiftUI and lifecycle methods are also selected as swiftui , The book name is "Practical Artificial intelligence with swift" by orielly code from the book page 1

page 2



Solution 1:[1]

You need to continue working through the tutorial before you'll be able to compile it. The next section of the book talks about creating Views.swift, and step 8 of that section includes this extension:

extension UIImage {
    func fixOrientation() -> UIImage? {
        UIGraphicsBeginImageContext(self.size)
        self.draw(at: .zero)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }

    var cgImageOrientation: CGImagePropertyOrientation {
        switch self.imageOrientation {
            case .up: return .up
            case .down: return .down
            case .left: return .left
            case .right: return .right
            case .upMirrored: return .upMirrored
            case .downMirrored: return .downMirrored
            case .leftMirrored: return .leftMirrored
            case .rightMirrored: return .rightMirrored
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rob mayoff