在 Swift 上为 Google ML Vision 框架旋转 UIImage 4

Rotating UIImage for Google ML Vision framework on Swift 4

捕获图像时,它默认为左方向。因此,当您将它放入 Google Vision framework 内的 textDetector 时,它会变得混乱,除非您将照片朝向左侧(右侧的主页按钮)。我希望我的应用支持两个方向。

let visionImage = VisionImage(image: image)

self.textDetector?.detect(in: visionImage, completion: { (features, error) in
    //2
    guard error == nil, let features = features, !features.isEmpty else {
        print("Could not recognize any text")
        self.dismiss(animated: true, completion: nil)
        return
    }

    //3
    print("Detected Text Has \(features.count) Blocks:\n\n")
    for block in features {
    //4
        print("\(block.text)\n\n")
    }
})

我已尝试以新方向重新创建图像,但不会改变它。

有人知道怎么办吗?

我已经尝试了所有这些建议

尝试规范化 UIImage 对象,使用这个函数:

import UIKit

public extension UIImage {    
    public func normalizeImage() -> UIImage {
        if self.imageOrientation == UIImageOrientation.up {
            return self
        }

        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))

        let normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        return normalizedImage
    }
}

我希望这对您有所帮助,因为我通常在出现方向问题时使用它。