CIFilter 后的图像旋转

Image rotating after CIFilter

我正在将 CIFilter 应用于肖像图像。由于某种原因,它顺时针旋转了 90 度。我怎样才能解决这个问题?我的代码如下

var imgOrientation = oImage.imageOrientation
var imgScale = oImage.scale


let originalImage = CIImage(image: oImage)

var filter = CIFilter(name: "CIPhotoEffect"+arr[sender.tag-1000])
filter.setDefaults()
filter.setValue(originalImage, forKey: kCIInputImageKey)

var outputImage = filter.outputImage
var newImage = UIImage(CIImage:outputImage, scale:imgScale, orientation:imgOrientation)


cameraStill.image = newImage

我猜问题出在这一行:

var newImage = UIImage(CIImage:outputImage, scale:imgScale, orientation:imgOrientation)

这不是将滤镜渲染到 UIImage 中的方式。您要做的是调用 CIContext(options: nil) 获取 CIContext,然后向该 CIContext 发送消息 createCGImage:fromRect: 以获取 CGImage。现在将该 CGImage 转换为 UIImage,并且在您这样做时,您可以应用您的方向。

你可以试试这个:

let cgImage = self.context.createCGImage(filterOutputImage,
                                         from: cameraImage.extent)!

let orientation: UIImage.Orientation =
      currentCameraType == AVCaptureDevice.Position.front ?
           UIImage.Orientation.leftMirrored:
           UIImage.Orientation.right

let image = UIImage(cgImage: cgImage,
                    scale: 1.0,
                    orientation: orientation)