'iOS Zoom/Distort a particular part of the image using CIFilter

The use case is to detect something in an image and zoom/distort-outward it as in you are looking through a magnifying glass.

Now I know the points where to zoom in but I need a CIFilter which can do the same.

Tried CIHoleDistortion but it did not work. The documentation seems to be fine to me and it should work but it only creates a black hole and the area around it is distorted.

let distortion = CIFilter(name: "CIHoleDistortion")
distortion.setValue(sourceImage, forKey: kCIInputImageKey)
distortion.setValue(CIVector.init(cgPoint: CGPoint.init(x: 200, y: 200)), forKey: "inputCenter")
distortion.setValue(NSNumber.init(value: 100), forKey: "inputRadius")

Here are the test results: enter image description here



Solution 1:[1]

Another (and maybe better) option is to use CIGlassLozenge filter:

  1. Set inputRefraction to something slightly larger than 1 (e.g. 1.06)

  2. Align both inputPoint0 and inputPoint1 with the coordinate of the zoom point

  3. Play with the radius to get the desired effect. The attached example uses radius 1000.

enter image description here

If you want to play with this filter (and any other out of 250 CIFilters) in real time check this app out: https://apps.apple.com/us/app/filter-magic/id1594986951

Solution 2:[2]

CIBumpDistortion as mentioned by Frank above, but the scale parameter actually works as follows: scale < 0 -> concave; scale > 0 -> convex.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s
Solution 2 coughski