iOS离屏渲染的原因

cornerRadius和masksToBounds组合探究离屏渲染

我们可以在模拟器下,开启color Off-screen Rendered 查看是否有离屏渲染


image

UIView 下会出现离屏渲染吗?

Simulator Screen Shot - iPhone SE (2nd generation) - 2020-07-07 at 15.43.35.png
  UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 100, 100)];
    view1.backgroundColor = [UIColor redColor];
    view1.layer.cornerRadius = 50;
    view1.layer.masksToBounds = YES;
    [self.view addSubview:view1];
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
    view2.backgroundColor = [UIColor redColor];
    view2.layer.cornerRadius = 50;
    view2.layer.masksToBounds = YES;
    view2.layer.contents = (__bridge  id)[UIImage imageNamed:@"btn"].CGImage;
    [self.view addSubview:view2];

说明cornerRadius和masksToBounds并不是造成离屏渲染的罪魁祸首

UIImageView下的cornerRadius和masksToBounds

Simulator Screen Shot - iPhone SE (2nd generation) - 2020-07-07 at 16.37.04.png
  UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 10, 100, 100)];
    imageView1.image  = [UIImage imageNamed:@"btn"];
    imageView1.layer.cornerRadius = 50;
    imageView1.layer.masksToBounds = YES;
    [self.view addSubview:imageView1];
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
    imageView2.image  = [UIImage imageNamed:@"btn"];
    imageView2.backgroundColor = [UIColor brownColor];
    imageView2.layer.cornerRadius = 50;
    imageView2.layer.masksToBounds = YES;
    [self.view addSubview:imageView2];

UIButton下cornerRadius和masksToBounds

Simulator Screen Shot - iPhone SE (2nd generation) - 2020-07-07 at 16.49.15.png
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn1.frame = CGRectMake(100, 10, 100, 100);
    btn1.layer.cornerRadius = 50;
    [self.view addSubview:btn1];
    [btn1 setImage:[UIImage imageNamed:@"hear.png"] forState:UIControlStateNormal];
    btn1.layer.masksToBounds = YES;
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake(100, 150, 100, 100);
    btn2.backgroundColor = [UIColor brownColor];
    btn2.layer.cornerRadius = 50;
    btn2.layer.masksToBounds = YES;
    [self.view addSubview:btn2];

触发离屏渲染的原因不能都"归功"于masksToBounds和cornerRadius,其实是图层和它们俩的共同作用。
UIView 只设置颜色背景颜色不会触发离屏渲染,在设置layer的contents属性后触发离屏渲染,在设置contents时应该会有一个图层来接受contents的内容,在需要masksToBounds和cornerRadius触发离屏渲染。另外只设置cornerRadius也都不会触发离屏渲染。
UIImageView 在只设置image时不会触发离屏渲染,只有在同时设置backgroundColor时才会触发离屏渲染。
UIButton 在只设置的backgroundColor时不会触发离屏渲染,在只设置image时会触发离屏渲染,从下图可知承载image的UIImageView也可能是懒加载,承载backgroundColor的layer是一直存在的,应该是UIButton的layer。


image.png

为什么UIView UIButton UIImageView 会在cornerRadius和masksToBounds的某些特定情况下会出现离屏渲染呢?

cornerRadius让图层产生圆角,masksToBounds是主控件关联的layer也产生圆角效果。UIView在设置layer.contents等于是UIImageView.UIImageView 在有背景和image同时存在的情况下,生成圆角,需要分别对image和背景都做渲染加圆角渲染好了再重新组合,再重新组合之前,我们要把渲染出来的图像保存起来,等两个都渲染好了再去组合放到帧缓存区,保存起来的地方就是离屏缓存区,这就是离屏渲染产生的过程。
正常的渲染流程
App->FrameBuffer->Display
离屏渲染下的渲染过程
APP->offScreenBuffer->FrameBuffer->Display

离屏渲染在项目中的运用

shouldRasterize会触发离屏渲染,在tableview中滑动视图会重新渲染cell,我们可以使用shouldRasterize,让cell的渲染结果缓存起来,不用每次都要重绘。但是在使用shouldRasterize需要注意一下几点

1.如果layer不能被复用,则没有必要打开光栅化。
2.如果layer不是静态的,需要被频繁修改,比如处于动画之中,那么开启离屏渲染反而影响效率。
3.离屏渲染缓存内容有时间限制,缓存内容100ms内如果没有被使用,那么它就会丢弃,无法进行复用。
3.离屏渲染缓存空间有限,超过2.5倍屏幕像素大小的话,也会失效,且无法进行复用。

常见触发离屏渲染的几种情况

1.使用mask的layer。

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn"]];
    imageView.frame = CGRectMake(50, 50, 100, 100);
    [self.view addSubview:imageView];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 100, 100);
    layer.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) radius:50 startAngle:0 endAngle:M_PI*2 clockwise:YES].CGPath;
    imageView.layer.mask = layer;

2.需要进行裁剪的layer(layer.masksToBounds/view.clipsToBounds)

   UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn1.frame = CGRectMake(100, 10, 100, 100);
    btn1.layer.cornerRadius = 50;
    [self.view addSubview:btn1];
    [btn1 setImage:[UIImage imageNamed:@"hear.png"] forState:UIControlStateNormal];

3.设置了组透明度为YES,并且透明度不为1的layer(layer.allowsGroupOpacity/layer.opacity)

   UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    view1.backgroundColor = [UIColor grayColor];
    view1.layer.allowsGroupOpacity = YES;
    view1.layer.opacity = 0.5;
    [self.view addSubview:view1];
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    view2.backgroundColor = [UIColor redColor];
    [view1 addSubview:view2];

4.添加了投影的layer(layer.shadow)

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    view1.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view1];
    view1.layer.shadowColor = [UIColor redColor].CGColor;
    view1.layer.shadowOpacity = 0.5;
    view1.layer.shadowOffset =CGSizeMake(0, 3);

5.采用光栅化的layer(layer.shouldRasterize)

 cell.layer.shouldRasterize = YES;
 cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

6.绘制了文字的layer(UILabel,CATextLayer,CoreText)

7.毛玻璃效果(UIVisualEffect)

    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView * effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.backgroundColor = [UIColor redColor];
    effectView.frame = CGRectMake(10, 10, 100, 100);
    [self.view addSubview:effectView];

离屏渲染中父视图背景颜色为clearColor

    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    firstView.layer.cornerRadius = 100;
    firstView.layer.masksToBounds = YES;
    [self.view addSubview:firstView];
    UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(75, 75, 50, 50)];
    secondView.backgroundColor = [UIColor redColor];
    [firstView addSubview:secondView];
    
     UIView *firstView2 = [[UIView alloc] initWithFrame:CGRectMake(50, 300, 200, 200)];
     firstView2.layer.cornerRadius = 100;
     firstView2.layer.masksToBounds = YES;
     [self.view addSubview:firstView2];
     UIView *secondView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
     secondView2.backgroundColor = [UIColor redColor];
     [firstView2 addSubview:secondView2];
image.png

父视图背景颜色为空时,当子视图大小或者位置不受父视图影响时不产生离屏渲染,当子视图大小或者位置受到父视图圆角效果影响时,产生离屏渲染。
推测:当子视图的大小或者位置不受父视图圆角影响时,且父视图背景颜色为空时子视图不需要做圆角处理,所以渲染的时候不需要分开渲染后合并处理。当子视图的大小或者位置受父视图的圆角影响时则需要分开渲染后合并处理故产生离屏渲染效果。用UIButton验证一下。

   UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor clearColor];
    button.frame = CGRectMake(60,50, 100, 100);
    button.layer.cornerRadius = 50;
    button.clipsToBounds = YES;
    button.titleLabel.font = [UIFont systemFontOfSize:8];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitle:@"123" forState:UIControlStateNormal];
    [self.view addSubview:button];

此UIButton不会产生离屏渲染。背景颜色为clearColor时,在OpenGL EL/Metal 是否会有渲染(推测会有渲染),在以后的研究中需要验证。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354