CIFilter后旋转的图像

时间:2015-05-31 14:24:21

标签: swift ciimage

我将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

2 个答案:

答案 0 :(得分:8)

我猜这个问题就在这一行:

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

这不是您如何将过滤器渲染为UIImage。你想要做的是调用CIContext(options: nil)来获取CIContext,然后发送消息createCGImage:fromRect:的CIContext以获得CGImage。现在将CGImage转换为UIImage,并且在执行此操作时,您可以应用您的方向。

答案 1 :(得分:0)

您可以尝试以下方法:

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)