Search code examples
iosswiftimage-processinguiimagegpuimage

Filter not apply properly in GPUImageBulgeDistortionFilter


I want to make tapped image area zoom.

When i apply GPUImageBulgeDistortionFilter to image it works properly at center point (0.5 , 0.5) but when center point changed the effect on image is not applying as given center point.

Image Square Working properly.

Image Width > Image Height. center point (0.5, 0.2) apply on (0.5, 0.1) and center point (0.5, 0.8)apply on (0.5, 0.9)

Image Width < Image Height. center point (0.5, 0.2) apply on (0.5, 0.3) and center point (0.5, 0.8) apply on (0.5, 0.7)

func tapGesture(_ sender: Any){
    let points = tap.location(ofTouch: 0, in: ivPic)
    let stillImageFilter = GPUImageBulgeDistortionFilter()
    stillImageFilter.center = CGPoint(x: points.x / ivPic.frame.size.width, y: points.y / ivPic.frame.size.height)
    stillImageFilter.radius = 0.1
    stillImageFilter.scale = 0.1
    ivPic.image = stillImageFilter.image(byFilteringImage: ivPic.image)
}

Solution

  • Try this

    func tapGesture(_ sender: Any) {
    
        let points = tap.location(in: ivPic)
        let stillImageFilter = GPUImageBulgeDistortionFilter()
        let aspectRatio = ivPic.frame.size.height / ivPic.frame.size.width
        stillImageFilter.center = CGPoint(x: points.x / ivPic.frame.size.width, y: ((((points.y / ivPic.frame.size.height) * aspectRatio) + 0.5) - (0.5 * aspectRatio)))
    
        stillImageFilter.radius = 0.1
        stillImageFilter.scale = 0.1
    
        ivPic.image = stillImageFilter.image(byFilteringImage: ivPic.image)
    }