How to make CIFilter's CIGlassDistortion texture's height match the image?

87 Views Asked by At

Despite of using the same ciImage for texture and image, on the result the texture has smaller height. Is there anything I'm missing in this code?

    var filter: CIFilter?
    
    switch style {
    case "gaussian":
        filter = CIFilter(name: "CIGaussianBlur")
        filter?.setValue(radius, forKey: kCIInputRadiusKey)
    case "motion":
        filter = CIFilter(name: "CIMotionBlur")
        filter?.setValue(radius, forKey: kCIInputRadiusKey)
    case "glass":
        filter = CIFilter(name: "CIGlassDistortion")
        filter?.setValue(radius * 100, forKey: kCIInputScaleKey)
        filter?.setValue(ciImage, forKey: "inputTexture")
        filter?.setValue(CIVector(x: ciImage.extent.size.width, y: ciImage.extent.size.height), forKey: kCIInputCenterKey)
    default:
        return
    }
    
    filter?.setValue(ciImage, forKey: kCIInputImageKey)
    
    guard let outputImage = filter?.outputImage else { return }

enter image description here

1

There are 1 best solutions below

0
Frank Rupprecht On

Some CIFilters return images with greater extent than the input image, like CIGaussianBlur or CIGlassDistortion.

But you can crop the output back to the same extent as the input, so that they will match again:

guard let outputImage = filter?.outputImage.cropped(to: ciInput.extent) else { return }