在项目开发中,我们经常会遇到使用模糊的图片作为背景的情况,如图:
苹果官方就提供了方法去实现:
//加载图片 创建imageView
UIImage *image = [UIImage imageNamed:@"psb.png"];
UIImageView * imageview = [[UIImageView alloc] init];
imageview.contentMode = UIViewContentModeScaleAspectFit;
imageview.frame = CGRectMake(0, 80, 320, 320);
[self.view addSubview:imageview];
//转换图片
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *midImage = [CIImage imageWithData:UIImagePNGRepresentation(image)];
//图片开始处理
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:midImage forKey:kCIInputImageKey];
//value 改变模糊效果值
[filter setValue:@7.0f forKey:@"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGImageRef outimage = [context createCGImage:result fromRect:[result extent]];
//转换成UIimage
UIImage *resultImage = [UIImage imageWithCGImage:outimage];
imageview.image = resultImage;
方法不止此一种,各位多多指点