iOS离屏渲染

一.在iOS开发的过程当中或多或少都会碰到离屏渲染那么造成离屏渲染的原因有以下几点 (总结不全望见谅)

1.使用了 mask 的 layer (layer.mask)
2.需要进行裁剪的 layer (layer.masksToBounds / view.clipsToBounds)
3.设置了组透明度为 YES,并且透明度不为 1 的 layer (layer.allowsGroupOpacity/ layer.opacity)
4.添加了投影的 layer (layer.shadow*)
5.采用了光栅化的 layer (layer.shouldRasterize)
6.绘制了文字的 layer (UILabel, CATextLayer, Core Text 等)

二.渲染流程

正常渲染流程


image.png

离屏渲染流程


image.png

圆角触发离屏渲染
image.png

view.layer.masksToBounds = true // 触发离屏渲染的原因


image.png

一下为处理圆角的手段

_imageView.clipsToBounds=YES;
_imageView.layer.cornerRadius=4.0;
 -(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {  
      UIGraphicsBeginImageContext(image.size);  
      CGContextRef context = UIGraphicsGetCurrentContext();  
      CGContextSetLineWidth(context, 2);  
      CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);  
      CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);  
      CGContextAddEllipseInRect(context, rect);  
      CGContextClip(context);  
        
     [image drawInRect:rect];  
     CGContextAddEllipseInRect(context, rect);  
     CGContextStrokePath(context);  
     UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();  
     UIGraphicsEndImageContext();  
     return newimg;  
 } 

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