5

我正在将 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
4

2 回答 2

10

我猜问题出在这一行:

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

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

于 2015-05-31T16:01:14.930 回答
2

你可以试试这个:

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)
于 2019-08-10T10:28:58.587 回答