[code]关于圆角图片问题

CPU、GPU在渲染图片时是如何工作的?

计算机系统中CPU、GPU协同工作,CPU计算好显示的内容给GPU,GPU渲染完>成后将渲染结果提交到真缓冲区,所以两者之间相互协作。如果CPU替GPU干了>活,就会拖慢了UI层的FBS,这就是所谓的离屏渲染。

离屏渲染

CPU干了不擅长的GPU的活,导致拖慢了UI层面的FBS,而且离屏需要创建新的缓冲区和上下文的切换,因此消耗大量的性能。

生成圆角图片的常用方式

  • 方法1:
imgView.layer.cornerRadius = 10;
imgView.clipsToBounds = YES;//消耗性能
缺点:离屏渲染。
  • 方法2:正解
-(UIImage *)hyb_imageWithCornerRadius:(CGFloat)radius { 
CGRect rect = (CGRect){0.f, 0.f, self.size}; 
UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale); 
CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
CGContextClip(UIGraphicsGetCurrentContext()); 
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();
 return image;
}

你可能感兴趣的:([code]关于圆角图片问题)