Core Image
Core Image 是苹果用来简化图片处理的框架,Core Image 都提供了大量的滤镜(Filter),这也是 Core Image 库中比较核心的东西之一。按照官方文档记载,在 OS X 上有 120 多种 Filter,而在 iOS 上也有 90 多种。
创建高斯模糊效果的图片
- (UIImage *)filterImage:(UIImage *)image blurLevel:(CGFloat)blur {
// 创建属性
CIImage *ciImage = [[CIImage alloc] initWithImage:image];
// 滤镜效果 高斯模糊
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:ciImage forKey:kCIInputImageKey];
// 指定模糊值 默认为10, 范围为0-100
[filter setValue:[NSNumber numberWithFloat:blur] forKey:@"inputRadius"];
// 生成图片
CIContext *context = [CIContext contextWithOptions:nil];
// 创建输出
CIImage *result = [filter valueForKey:kCIOutputImageKey];
// 生成CGImageRef
CGImageRef outImage = [context createCGImage: result fromRect:CGRectMake(0, 0, kScreenWidth * 0.8, kScreenHeight * 0.4)];
UIImage * blurImage = [UIImage imageWithCGImage:outImage];
return blurImage;
}
UIVisualEffectView
UIVisualEffect - UIBlurEffect和UIVibrancyEffect
UIBlurEffect对象用于将blur(毛玻璃)效果应用于UIVisualEffectView视图下面的内容。不过,这个对象的效果并不影响UIVisualEffectView对象的contentView中的内容。
UIVibrancyEffect主要用于放大和调整UIVisualEffectView视图下面的内容的颜色,同时让UIVisualEffectView的contentView中的内容看起来更加生动。
// 创建UIBlurEffect
UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView * blurEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
blurEffectView.frame = CGRectMake(kScreenWidth * 0.25, 180, kScreenWidth * 0.5, 80);
blurEffectView.layer.cornerRadius = 10.0f;
blurEffectView.layer.masksToBounds = YES;
[self.view addSubview:blurEffectView];
// 创建UIVibrancyEffect
UIVibrancyEffect * vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)blurEffectView.effect];
UIVisualEffectView * vibrancyEffectView = [[UIVisualEffectView alloc]initWithEffect:vibrancyEffect];
vibrancyEffectView.frame = blurEffectView.bounds;
//使用自动布局的时候设置为NO
//[vibrancyEffectView setTranslatesAutoresizingMaskIntoConstraints:NO];
[blurEffectView.contentView addSubview:vibrancyEffectView];
// 创建UILabel
UILabel * label = [[UILabel alloc]init];
label.text = @"Hello World!";
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0];
[label sizeToFit];
label.center = vibrancyEffectView.center;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
[vibrancyEffectView.contentView addSubview:label];//透明效果
//[blurEffectView.contentView addSubview:label];//白色效果