以前設置圖片圓角寸潦,都是用下面這幾句代碼輕松搞定
imgView.layer.cornerRadius = 10;
// 這一行代碼是很消耗性能的
imgView.clipsToBounds = YES;
這在實現(xiàn)功能上沒有什么不同舱禽,但是在實現(xiàn)瀑布流這種大批量圖片圓角處理時就會出現(xiàn)頁面卡頓現(xiàn)象;
所以呢舞萄,問題來了眨补。這時候有什么好的方法解決呢
別急!5古А3怕荨!馬上呈上解決方案
怎么辦呢崎弃?
方法一:(這個方法一定要寫在Image的分類里面甘晤,并且圓角大小自己可控)
(UIImage *)yye_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;
}
方法二:(這時候自己想要一個切成圓的圖片)
/* 設置圓形圖片(放到分類中使用) /
(UIImage *)yye_cutCircleImage {
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
// 獲取上下文
CGContextRef ctr = UIGraphicsGetCurrentContext();
// 設置圓形
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextAddEllipseInRect(ctr, rect);
// 裁剪
CGContextClip(ctr);
// 將圖片畫上去
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}