CPU、GPU在渲染圖片時是如何工作的?
計算機系統(tǒng)中CPU、GPU協(xié)同工作,CPU計算好顯示的內容給GPU,GPU渲染完>成后將渲染結果提交到真緩沖區(qū),所以兩者之間相互協(xié)作。如果CPU替GPU干了>活,就會拖慢了UI層的FBS,這就是所謂的離屏渲染。
離屏渲染
CPU干了不擅長的GPU的活,導致拖慢了UI層面的FBS,而且離屏需要創(chuàng)建新的緩沖區(qū)和上下文的切換,因此消耗大量的性能。
生成圓角圖片的常用方式
- 方法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;
}