How to implement a box or gaussian blur on iOS

willc2 picture willc2 · Jul 16, 2009 · Viewed 21.9k times · Source

I want to be able to take an image and blur it relatively quickly (say in 0.1 sec). Image size would almost never be larger than 256 x 256 px.

Do I have to loop thru every pixel and average them with neighbors or is there a higher-level way that I could do this?

PS: I am aware that multiple box blurs can approximate a gaussian blur.

Answer

Gabe picture Gabe · Nov 22, 2010

I found a really fast pretty crappy way for iOS3.2+ apps

  UIView *myView = [self view];
  CALayer *layer = [myView layer];
  [layer setRasterizationScale:0.25];
  [layer setShouldRasterize:YES];

This rasterizes the view down to 4x4 pixel chunks then scales it back up using bilinear filtering... it's EXTREMELY fast and looks ok if you are just wanting to blur a background view under a modal view.

To undo it, just set the rasterization scale back to 1.0 or turn off rasterization.