【发布时间】: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)
}
}
这是对应的截图:
【问题讨论】: