我正在对肖像图像应用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)