模糊效果(毛玻璃效果)

模糊效果(毛玻璃效果)


代码位置:https://github.com/sunyanyan/blurredImageSample

效果演示:

1. 使用iOS自带的 UIImage+ImageEffects 文件

apple官方UIImage+ImageEffects文件位置

文件中有这么几个方法:

- (UIImage *)applyLightEffect;
- (UIImage *)applyExtraLightEffect;
- (UIImage *)applyDarkEffect;
- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;

- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;

2. 使用 CoreImage 中的模糊滤镜

  1. coreImage是苹果用来简化图片处理的框架
  2. CIImage, CIFilter与CIContext三者之间的联系(CoreImage中三个重要的类)

示例:

-(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;
}

3. 使用 UIVisualEffectView (实时)(iOS8)

//生成该对象
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];	//然后添加将其添加到相应的UIView 之上
posted @ 2016-04-29 14:09  孙焱焱  阅读(982)  评论(0编辑  收藏  举报