美文网首页
OC-圆角和离屏渲染

OC-圆角和离屏渲染

作者: 紫云夕月 | 来源:发表于2021-09-08 15:31 被阅读0次

圆角触发离屏渲染的真正原因

当我们设置了cornerRadius以及masksToBounds进行圆角+裁剪时,masksToBounds裁剪属性会应用到所有的图层上。

本来我们从后往前绘制,绘制完一个图层就可以丢弃了。但现在需要依次在 Offscreen Buffer中保存,等待圆角+裁剪处理,即引发了离屏渲染 。

一旦我们 为contents设置了内容 ,无论是图片、绘制内容、有图像信息的子视图等,再加上圆角+裁剪,就会触发离屏渲染。

只有 单层 内容需要添加圆角和裁切,可以不需要用到离屏渲染技术。但如果加上了背景色、边框或其他有图像内容的图层,就会产生为 多层 添加圆角和裁切,所以还是会触发离屏渲染

//UIImageView只有单层内容需要添加圆角和裁切,不会触发离屏渲染
iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ImageIcon"]];
iconView.frame = CGRectMake(20, 5, 100, 50);
iconView.layer.cornerRadius = 25;
iconView.layer.masksToBounds = YES;
[self.view addSubview:iconView];
//UIImageView一旦我们为contents设置了内容 ,无论是图片、绘制内容、有图像信息的子视图等,再加上圆角+裁剪,就会触发离屏渲染。

//添加背景
iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ImageIcon"]];
iconView.backgroundColor = UIColor.redColor;
iconView.frame = CGRectMake(20, 5, 100, 50);
iconView.layer.cornerRadius = 25;
iconView.layer.masksToBounds = YES;
[self.view addSubview:iconView];

//或添加边框
iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ImageIcon"]];
iconView.layer.borderWidth = 2.0;
iconView.layer.borderColor = UIColor.redColor.CGColor;
iconView.frame = CGRectMake(20, 5, 100, 50);
iconView.layer.cornerRadius = 25;
iconView.layer.masksToBounds = YES;
[self.view addSubview:iconView];

//或添加 图像信息的子视图
ceshiView = [UIView new];
ceshiView.backgroundColor = [UIColor redColor];
ceshiView.frame = CGRectMake(0, 0, 50, 50);
[iconView addSubview:self.ceshiView];
//为UIButton添加圆角和裁剪,则会触发离屏渲染。
conButton = [UIButton new];
[iconButton setImage:[UIImage imageNamed:@"ImageIcon"] forState:UIControlStateNormal];
iconButton.frame = CGRectMake(150, 5, 100, 50);
iconButton.layer.cornerRadius = 25;
iconButton.layer.masksToBounds = YES;
[self.view addSubview:iconButton];
//如果改为UIButton中的UIImageView添加圆角和裁剪,则 不会触发离屏渲染
iconButton = [UIButton new];
[iconButton setImage:[UIImage imageNamed:@"ImageIcon"] forState:UIControlStateNormal];
iconButton.frame = CGRectMake(150, 5, 100, 50);
iconButton.imageView.layer.cornerRadius = 25;
conButton.imageView.layer.masksToBounds = YES;
[self.view addSubview:iconButton];

相关文章

网友评论

      本文标题:OC-圆角和离屏渲染

      本文链接:https://www.haomeiwen.com/subject/syxuvltx.html