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

问题描述 投票:0回答:1

捕获图像时,默认为左方向。所以,当你把它喂入textDetector里面的Google Vision framework时,它会变得混乱,除非你把照片朝左(右边的主页按钮)。我希望我的应用程序支持这两种方向。

enter image description here

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

我试图用新的方向重新创建图像,但不会改变它。

有谁知道该怎么办?

我已经尝试了所有这些建议How to rotate image in Swift?

ios swift apple-vision
1个回答
1
投票

尝试使用此函数规范化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
    }
}

我希望这会有所帮助,因为每当出现方向问题时我都会使用它。

© www.soinside.com 2019 - 2024. All rights reserved.