【iOS开发】生成高斯模糊效果背景

【iOS开发】生成高斯模糊效果背景,第1张

做开发时,总是使用系统默认的白色背景会显得有些生硬,所以当我们以展示图片为目的时,不妨将图片放大、再做高斯模糊处理以作为背景。

我把这个处理过程用 Swift 封装成了一个函数,供大家参考。

三个参数分别为:image(原始清晰图片)、view(你需要将生成的模糊背景插入在这个view的下层当做背景)、blurRadius(高斯模糊处理的模糊半径)

其中 let context = CIContext(options: nil)

这一句,在真机测试时,会引起控制台报错

这是苹果的一个 Bug ,想要回避这个信息输出可以用下面这一句进行替换:

let context = CIContext(options:[kCIContextUseSoftwareRenderer : true])

这句可以使图片渲染工作在 CPU 而非 GPU 完成,从而绕过这个 Bug,但是会引起效率下降,耗时大大增加,不推荐使用。

使用效果展示:

1.通过按钮的事件来设置背景色

- (void)viewDidLoad {

[super viewDidLoad]

UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 50)]

[button1 setTitle:@"button1" forState:UIControlStateNormal]

button1.backgroundColor = [UIColor orangeColor]

[button1 addTarget:self action:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown]

[button1 addTarget:self action:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside]

[self.view addSubview:button1]

}

// button1普通状态下的背景色

- (void)button1BackGroundNormal:(UIButton *)sender

{

sender.backgroundColor = [UIColor orangeColor]

}

// button1高亮状态下的背景色

- (void)button1BackGroundHighlighted:(UIButton *)sender

{

sender.backgroundColor = [UIColor greenColor]

}

在开发中我们经常会遇到设置透明背景的需求,在一个View 上面有 多个subView,而这个View是透明的,通常我们习惯于这样

这是你运行程序会发现上面的子视图也透明了,你顿时只能呵呵了,这时候有一些人则要UI 切图来解决了,其实还有一种很简单的解决方案

我们给颜色通道设置一个透明度就可以解决这个问题了,有兴趣的可以试试。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12098985.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存