通常咱們在iOS開發的過程當中設置圓角都是以下這樣設置的。ios
imageView.clipsToBounds = YES; [imageView.layer setCornerRadius:50];
這樣設置會觸發離屏渲染,比較消耗性能。好比當一個頁面上有十幾頭像這樣設置了圓角
緩存
會明顯感受到卡頓。
注意:ios9.0以後對UIImageView的圓角設置作了優化,UIImageView這樣設置圓角
不會觸發離屏渲染,ios9.0以前仍是會觸發離屏渲染。而UIButton仍是都會觸發離屏渲染。
imageView.clipsToBounds = YES; imageView.layer setCornerRadius:50];
imageView.layer.shouldRasterize = YES;
shouldRasterize=YES設置光柵化,可使離屏渲染的結果緩存到內存中存爲位圖, 使用的時候直接使用緩存,節省了一直離屏渲染損耗的性能。性能
可是若是layer及sublayers經常改變的話,它就會一直不停的渲染及刪除緩存從新 建立緩存,因此這種狀況下建議不要使用光柵化,這樣也是比較損耗性能的。優化
第三種spa
這種方式性能最好,可是UIButton上不知道怎麼繪製,能夠用UIimageView添加個 點擊手勢當作UIButton使用code
UIGraphicsBeginImageContextWithOptions(avatarImageView.bounds.size, NO, [UIScreen mainScreen].scale); [[UIBezierPath bezierPathWithRoundedRect:avatarImageView.bounds cornerRadius:50] addClip]; [image drawInRect:avatarImageView.bounds]; avatarImageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
這段方法能夠寫在SDWebImage的completed回調裏,也能夠在UIImageView+WebCache.h 裏添加一個方法,isClipRound判斷是否切圓角,把上面繪製圓角的方法封裝到裏面。blog