【问题标题】:How to apply image orientation to a CIImage?如何将图像方向应用于 CIImage?
【发布时间】:2020-08-22 14:11:34
【问题描述】:

我尝试使用 CIFilter 合成 2 张图像,但无法正确处理图像方向。

在下面的示例中,我捕获了相同的场景,但手机有 2 个不同的方向(第一张图像的顶部音量按钮,第二张图像的底部)。在以下屏幕截图中,第一张图片显示在顶部,第二张图片显示在中间。我验证了 2 个图像方向不同(参见 print 调用)。

SwiftUI 正确处理图像方向。如果我现在使用 CIFilter(第三张图像)合成 2 张图像,则不会考虑图像的方向并且合成不是我想要的(我想要并排放置 2 个杯子)。

这显然不是错误。但是如何在应用合成 CIFIlter 之前根据它们各自的方向旋转输入图像?这样做的标准方法是什么?

感谢您的帮助。

下面是这个简单例子对应的代码:

import SwiftUI

struct ContentView: View {

    @State var input1 = UIImage(named: "image1.jpg")
    @State var input2 = UIImage(named: "image2.jpg")
    @State var output : UIImage?

    var body: some View {
        VStack {

            Image(uiImage: input1!)
                .resizable()
                .scaledToFit()

            Image(uiImage: input2!)
                .resizable()
                .scaledToFit()

            if output != nil {
                Image(uiImage: output!)
                    .resizable()
                    .scaledToFit()
            }

        }
        .onAppear(){
            self.compose()
        }
    }

    func compose() {

        print("Orientation of input1: \(input1!.imageOrientation.rawValue)")
        print("Orientation of input2: \(input2!.imageOrientation.rawValue)")

        let ciContext = CIContext(options: nil)

        let ciInput1 = CIImage(image: input1!)
        let ciInput2 = CIImage(image: input2!)

        let filter = CIFilter(name: "CILinearDodgeBlendMode")!
        filter.setValue(ciInput1, forKey: "inputImage")
        filter.setValue(ciInput2, forKey: "inputBackgroundImage")

        let ciOutput = filter.value(forKey: kCIOutputImageKey) as! CIImage

        let cgOutput = ciContext.createCGImage(ciOutput, from: ciOutput.extent)!

        output = UIImage(cgImage: cgOutput)
    }
}

这是对应的截图:

【问题讨论】:

    标签: swift cifilter


    【解决方案1】:

    您可以告诉 Core Image 在加载时考虑图像的方向:

    let ciInput1 = CIImage(image: input1!, options: [.applyOrientationProperty: true])
    let ciInput2 = CIImage(image: input2!, options: [.applyOrientationProperty: true])
    

    编辑:

    所以上面的方法似乎只适用于从 URL 或数据加载图像,而不是从 UIImage。

    不过,您可以自己应用方向。这是一个方便的扩展:

    extension UIImage.Orientation {
        var exifOrientation: Int32 {
            switch self {
                case .up: return 1
                case .down: return 3
                case .left: return 8
                case .right: return 6
                case .upMirrored: return 2
                case .downMirrored: return 4
                case .leftMirrored: return 5
                case .rightMirrored: return 7
            }
        }
    }
    

    然后你可以像这样变换图像:

    let ciInput1 = CIImage(image: input1!).oriented(forExifOrientation: image1.imageOrientation.exifOrientation)
    let ciInput2 = CIImage(image: input2!).oriented(forExifOrientation: image2.imageOrientation.exifOrientation)
    

    【讨论】:

    • 感谢您的回答。但我可能做错了什么,因为我发现结果没有变化。
    • 嗯...您能检查一下图像的元数据中写入的方向吗?
    • 使用 XCode:orientation 1=0, orientation 2=1,使用预览版 (macOS):orientation1=1, orientation 2=3。
    猜你喜欢
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多