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 是否会有渲染(推测会有渲染),在以后的研究中需要验证。

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