离屏渲染

什么叫离屏渲染:

当GPU无法直接把渲染结构写入帧缓冲区,而是先创建一个临时的区域,之后再写入帧缓冲区,这个过程叫离屏渲染

什么会触发离屏渲染:

1.光栅

UIImageView*imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(50,50,100,100)];

imgView.image= [UIImageimageNamed:@"logo"];

 imgView.layer.shouldRasterize = YES;

  [self.viewaddSubview:imgView];

2.遮罩

UIImageView*imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(50,50,100,100)];

    imgView.image= [UIImageimageNamed:@"logo"];

    [self.viewaddSubview:imgView];

    CALayer*layer = [CALayerlayer];

    layer.frame=CGRectMake(30,30,100,100);

    layer.backgroundColor = [UIColor redColor].CGColor;

    imgView.layer.mask= layer;


3.阴影,如果alpha=1则不会造成离屏渲染,可以通过设置shadowPath来避免离屏渲染

UIImageView*imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(50,50,100,100)];

    imgView.image= [UIImageimageNamed:@"logo"];

    imgView.layer.shadowColor = [UIColor redColor].CGColor;

    imgView.layer.shadowOffset = CGSizeMake(20,20);

    imgView.layer.shadowOpacity = 0.1;

    [self.viewaddSubview:imgView];

4.不透明,有子视图,并且子视图有内容,会造成离屏渲染

UIImageView*imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(50,50,100,100)];

    imgView.image= [UIImageimageNamed:@"logo"];

    [self.viewaddSubview:imgView];

    imgView.alpha = 0.5;//alpha=1不会触发离屏渲染

    imgView.layer.allowsGroupOpacity = YES;

    UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,20,20)];

    view.backgroundColor = [UIColor whiteColor];

    [imgViewaddSubview:view];

5.UIView设置圆角,有子视图,且子视图有内容,且设置了ClickToBounds=YES则会造成离屏渲染,可以不设置ClickToBounds也可以造成圆角效果,且不会造成离屏渲染

6.UILabel设置圆角,不会造成离屏渲染

7.UIImageView设置圆角,不设置背景色不会造成离屏渲染,设置背景色会

8.UIButton设置圆角,单文本不会造成离屏渲染,设置背景图片会造成离屏渲染

你可能感兴趣的:(离屏渲染)