毛玻璃效果的几种实现方式

在开发过程中,我们有时候会遇到一些需求,就是对图片进行处理,达到毛玻璃效果。

处理方式一:

直接用UIToolBar进行处理,因为UIToolBar自带毛玻璃处理效果。如下执行方法所示。

-(void)initBackImage{

    self.personalImgeBlur.image =[UIImage imageNamed:@"xxx.png"];

    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.personalImgeBlur.frame.size.height)];

 [self.personalImgeBlur addSubview:toolbar];

}

图1

如图1所示,中间的图为原始图,背景图就是使用UIToolBar后的效果图,从图片可以看出,效果并不理想,原始图的轮廓都看不清楚了。

为了改变效果,可以通过修改toolBar的barStyle属性来实现不同风格的毛玻璃效果:其取值范围有UIBarStyleDefault、UIBarStyleBlack、UIBarStyleBlackOpaque和UIBarStyleBlackTranslucent。下面我们使用UIBarStyleBlackTranslucent来设置效果。toolbar.barStyle = UIBarStyleBlackTranslucent。

图2

图2效果要比图1的效果好。我们再在此基础上,设置toolbar.alpha=0.6;效果如图3所示。

图3

从该图可以看出,具体的图片轮廓分明,效果比较好。

处理方式二:

 自iOS 8之后,新增了UIBlurEffect 类和 UIVisualEffectView 类,可以用来实现毛玻璃效果。代码和效果如下:

-(void)initBackImage{

    self.personalImgeBlur.image = self.personalImage.image;

    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];

    effectview.frame = self.personalImgeBlur.frame;

    [self.personalImgeBlur addSubview:effectview];

}

图4

当然,和UIToolBar类似,也可以修改参数,达到不同的效果。具体设置参数有如下几种,UIBlurEffectStyleExtraLight、UIBlurEffectStyleLight和UIBlurEffectStyleDark。设置方式和UIToolBar类似,此处就不一一罗列。

方法三:

Core Image方法,直接看代码

-(void)initBackImage{

    UIImageView *blurImageView = [[UIImageView alloc]initWithFrame:self.personalImgeBlur.bounds];

    blurImageView.image= [selfblur:self.personalImage.image];

    [self.personalImgeBlur addSubview:blurImageView];

}

- (UIImage*)blur:(UIImage*)theImage{

    CIContext*context = [CIContextcontextWithOptions:nil];

    CIImage*inputImage = [CIImageimageWithCGImage:theImage.CGImage

    CIFilter*filter = [CIFilterfilterWithName:@"CIGaussianBlur"];

    [filtersetValue:inputImage forKey:kCIInputImageKey];

    [filtersetValue:[NSNumber numberWithFloat:15.0f] forKey:@"inputRadius"];

    CIImage *result = [filter valueForKey:kCIOutputImageKey];

   CGImageRefcgImage = [contextcreateCGImage:resultfromRect:[inputImageextent]];

    UIImage*returnImage = [UIImageimageWithCGImage:cgImage];

    CGImageRelease(cgImage);

    returnreturnImage;

}

效果如图5所示

图5
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 话说苹果在iOS7.0之后,很多系统界面都使用了毛玻璃效果,增加了界面的美观性,比如下图的通知中心界面; 效果图:...
    Mr_董阅读 1,320评论 0 0
  • 许多UIView的子类,如一个UIButton或一个UILabel,它们知道怎么绘制自己。迟早,你也将想要做一些自...
    shenzhenboy阅读 1,692评论 2 8
  • 前一段时间项目中用到毛玻璃效果,那时对UIBlurEffect类和 UIVisualEffectView这两个类做...
    WheatDen阅读 7,034评论 1 2
  • 前言 为了分析mysql的性能,则需要大量的数据测试,还好mysql提供了这样的样板 环境 下载 下载地址,下载s...
    阿亮私语阅读 3,956评论 0 0
  • 这一次的流感真猛呀,希望你注意身体,不要不幸被传染。 一月中旬,我去逛了一趟商场,那天所有的商场暖气开的超强,羽绒...
    锦锦_d440阅读 293评论 0 10