代码位置:https://github.com/sunyanyan/blurredImageSamplehtml
效果演示:ios
apple官方UIImage+ImageEffects文件位置git
文件中有这么几个方法:github
- (UIImage *)applyLightEffect; - (UIImage *)applyExtraLightEffect; - (UIImage *)applyDarkEffect; - (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor; - (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;
示例:app
-(UIImage*)applyGaussianBlurImage:(UIImage*)image { // CIImage CIImage *ciImage = [[CIImage alloc] initWithImage:image]; // CIFilter(滤镜的名字, 不要写错 高斯模糊) CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; // 将图片输入到滤镜中 [blurFilter setValue:ciImage forKey:kCIInputImageKey]; /**在传入图片进入滤镜后,能够更改滤镜的一些参数进行设置,好比模糊程度等*/ // NSLog(@"%@", [blurFilter attributes]); // 打印看一下有哪些参数能够设置及相关信息 // inputRadius参数: 模糊的程度 默认为10, 范围为0-100, 接收的参数为NSNumber类型 // 设置模糊的程度 [blurFilter setValue:@(8) forKey:@"inputRadius"]; // 将处理好的图片输出 CIImage *outImage = [blurFilter valueForKey:kCIOutputImageKey]; // CIContext 上下文(参数nil->默认为CPU渲染, 若是想用GPU渲染来提升效率的话,则须要传参数) CIContext *context = [CIContext contextWithOptions:nil]; // 将处理好的图片建立出来 outImage原来的大小size CGImageRef outputCGImage = [context createCGImage:outImage fromRect:[outImage extent]]; UIImage *blurImage = [UIImage imageWithCGImage:outputCGImage]; // 释放CGImageRef CGImageRelease(outputCGImage); return blurImage; }
//生成该对象 UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]]; //而后添加将其添加到相应的UIView 之上