Advertisement

Blurry Images

iOS 7 introduced the UIBlurEffect, which was very cool, but not particularly controllable. Here’s a more granular approach discovered at work, but sadly, not used.

// *************************************************************************************************

#pragma mark

#pragma mark - helper methods

// this helper method processes a Gaussian Blur for image.

// We tried the Apple UIBlurEffect but not granular enough controls.

//  Usage in viewDidLoad (assuming you want the blur to appear immediately)

//    self.blurryProductImageView.image = self.productImage;

//

//    // more blurr => increase the number of loops

//

//    for (int i=0; i < 10; i++) {

//        self.blurryProductImageView.image = [self imageWithGaussianBlur:self.blurryProductImageView.image];

//    }

//    self.blurryProductImageView.alpha = 0.5;

- (UIImage *)imageWithGaussianBlur:(UIImage *)image {

float weight[5] = {0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162};

// Blur horizontally

UIGraphicsBeginImageContext(image.size);

[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[0]];

for (int x = 1; x < 5; ++x) {

[image drawInRect:CGRectMake(x, 0, image.size.width, image.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[x]];

[image drawInRect:CGRectMake(-x, 0, image.size.width, image.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[x]];

}

UIImage *horizBlurredImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

// Blur vertically

UIGraphicsBeginImageContext(image.size);

[horizBlurredImage drawInRect:CGRectMake(0, 0, image.size.width, image.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[0]];

for (int y = 1; y < 5; ++y) {

[horizBlurredImage drawInRect:CGRectMake(0, y, image.size.width, image.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[y]];

[horizBlurredImage drawInRect:CGRectMake(0, -y, image.size.width, image.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[y]];

}

UIImage *blurredImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//

return blurredImage;

}

Amazing feature, that sadly, my employer decided to take it out of the app.