简体   繁体   English

尝试使用CIFilter旋转/裁剪图像时,createCGImage返回nil

[英]createCGImage returns nil when attempting to rotate/crop an image using CIFilter

I am working on applying multiple CIFilters to an image but I keep getting a nil result when I apply the second filter. 我正在对一个图像应用多个CIFilter,但是当我应用第二个过滤器时,我一直得到零结果。 I've created crop and rotate functions as follows: 我创建了裁剪和旋转函数,如下所示:

func crop(_ image: CIImage) -> CIImage?{
    let cropRectangle = CGRect(x: 0, y: 0, width: 0.5*image.extent.width, height: 0.5*image.extent.height)
    guard let filter = CIFilter(name: "CICrop") else {print("Could not create filter.");return nil}
    filter.setValue(image, forKey: "inputImage")
    filter.setValue(cropRectangle, forKey: "inputRectangle")
    return filter.outputImage
}

func rotate(image: CIImage, rotation: CGFloat) -> CIImage?{
    guard let filter = CIFilter(name: "CIAffineTransform") else {print("Unable to generate filter");return nil}
    let rotationTransform = CGAffineTransform.init(rotationAngle: rotation)
    filter.setValue(image, forKey: "inputImage")
    filter.setValue(rotationTransform, forKey: "inputTransform")
    return filter.outputImage
}

If I apply crop and then rotation, my context.createCGImage works fine, but when I apply rotate and then crop, it returns nil. 如果我先应用裁剪然后旋转,我的context.createCGImage可以正常工作,但是当我先应用旋转然后裁剪时,它返回nil。 I have checked the .extension on the CIImage I am attempting to crop to make sure the crop rectangle is within its bounds. 我已经在尝试裁剪的CIImage上检查了.extension,以确保裁剪矩形在其范围内。 Accepting ideas. 接受想法。 Here's my call to the 2 above mentioned functtions: 这是我对上述2个功能的调用:

let context = CIContext(options: nil)
        guard let ciImage = CIImage(image: #imageLiteral(resourceName: "sample3")) else {fatalError("Error on image generation!")}
        guard let ciRotated = self.rotate(image: ciImage, rotation: CGFloat(Double.pi*3/2)) else {print("Could not rotate.");return}
        guard let ciCropped = self.crop(ciRotated) else {print("Error cropping.");return}
        guard let final = context.createCGImage(ciCropped, from: ciCropped.extent) else {print("Error on CG gen.");return}

The problem was that the origin of the rotated image was no longer at 0,0, so the crop rectangle was really out of bounds, making the crop function return a 0x0 sized image. 问题在于旋转后的图像的原点不再为0,0,因此裁剪矩形实际上不在边界内,从而使裁剪函数返回大小为0x0的图像。 I added a translation to make the origin return to 0,0 and everything worked. 我添加了一个翻译,以使原点返回0,0,一切正常。 Similar issue here: Core Image: after using CICrop, applying a compositing filter doesn't line up 此处类似的问题: 核心图片:使用CICrop后,应用合成滤镜后无法对齐

Translation function I've created: 我创建的翻译功能:

func translation(image: CIImage, x: CGFloat, y: CGFloat) -> CIImage?{
    guard let filter = CIFilter(name: "CIAffineTransform") else {print("Unable to generate filter"); return nil}
    let translate = CGAffineTransform.init(translationX: x, y: y)
    filter.setValue(image, forKey: "inputImage")
    filter.setValue(translate, forKey: "inputTransform")
    return filter.outputImage
}

Final call to this example: 最后调用此示例:

            let context = CIContext(options: nil)
        guard let ciImage = CIImage(image: #imageLiteral(resourceName: "sample3")) else {fatalError("Error on image generation!")}
        guard let ciRotated = self.rotate(image: ciImage, rotation: CGFloat(Double.pi*3/2)) else {print("Could not rotate.");return}
        guard let ciTranslated = self.translation(image: ciRotated, x: 0, y: ciRotated.extent.height) else {print("Unable to translate."); return}
        guard let ciCropped = self.crop(ciTranslated) else {print("Error cropping.");return}
        guard let final = context.createCGImage(ciCropped, from: ciCropped.extent) else {print("Error on CG gen.");return}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM