快速将滤镜应用于图像交换的高度和宽度

时间:2019-07-08 12:57:37

标签: uiimageview swift4 core-image cifilter

目前,我正在开发一个应用程序,在该应用程序中我可以捕获照片并将滤镜应用于照片。滤镜可以完美地应用,但是在应用滤镜之后,新的图像尺寸,高度,宽度将与捕获的图像交换。像这样

enter image description here

我无法弄清楚我在哪里犯错

  let ciContext = CIContext(options: nil)
    let coreimg = CIImage(image: AppDelegate.captured_iamge!)
    let filter = CIFilter(name: CIFilterNames[indexPath.row])
    filter!.setDefaults()
    filter!.setValue(coreimg, forKey: kCIInputImageKey)
    let filtered_img_data = filter!.value(forKey: kCIOutputImageKey) as! CIImage
    let filtered_img_ref = ciContext.createCGImage(filtered_img_data, from: filtered_img_data.extent)
    let img = UIImage(cgImage: filtered_img_ref!)
    img_view.image = img

如果有人可以帮忙,那就太好了

1 个答案:

答案 0 :(得分:1)

之所以会发生这种情况,是因为您输入的图像的方向(纵向)与相机的自然方向(横向)不同。

如果您询问UIImage的大小,则它已经考虑了其方向。不过,在引擎盖下,图像数据是从相机中存储的,这也是Core Image在操作的内容。因此,在您的过滤器链中,图像失去了其方向信息。

我认为您可以通过在创建时将信息传递到输出图像来解决此问题:

let inputImage = AppDelegate.captured_iamge!
//...
let img = UIImage(cgImage: filtered_img_ref!, scale: inputImage.scale, orientation: inputImage.orientation)