Skip to content

Instantly share code, notes, and snippets.

@Aufree
Created June 14, 2015 02:20
Show Gist options
  • Save Aufree/902f1576c58874db01ba to your computer and use it in GitHub Desktop.
Save Aufree/902f1576c58874db01ba to your computer and use it in GitHub Desktop.
@implementation ViewController
- (void)viewDidLoad{
_showCoverImage.contentMode = UIViewContentModeScaleAspectFit;
_showCoverImage.image = [self imageWithBlurredImageWithImage:_showCoverImage.image andBlurInsetFromBottom: 200 withBlurRadius:3];
}
- (UIImage*)imageWithBlurredImageWithImage:(UIImage*)image andBlurInsetFromBottom:(CGFloat)bottom withBlurRadius:(CGFloat)blurRadius{
UIGraphicsBeginImageContext(image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -image.size.height);
CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, bottom), [self blurImage: image withBottomInset: bottom blurRadius: blurRadius].CGImage);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage*)blurImage:(UIImage*)image withBottomInset:(CGFloat)inset blurRadius:(CGFloat)radius{
image = [UIImage imageWithCGImage: CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, image.size.height - inset, image.size.width,inset))];
CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage];
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:ciImage forKey:kCIInputImageKey];
[filter setValue:@(radius) forKey:kCIInputRadiusKey];
CIImage *outputCIImage = filter.outputImage;
CIContext *context = [CIContext contextWithOptions:nil];
return [UIImage imageWithCGImage: [context createCGImage:outputCIImage fromRect:ciImage.extent]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment