一般在給圖片做圓形處理時(shí)蝗茁,我們用得較多的可能就是 :
ImageView.layer.cornerRadius=Iv.frame.size.width *0.5 ; ImageView.layer.masksToBounds=YES;
這兩行代碼或許寫起來比較方便、容易記起來褐啡,又或許是其他什么原因启盛,當(dāng)然少量的圖片處理使用這個(gè)是完全沒問題的,但是,對(duì)這里有個(gè)“但是”抢肛,但是如果你需要同時(shí)對(duì)很多圖片進(jìn)行圓角處理的話,建議最好不要使用這種方案碳柱,因?yàn)樗鼘?duì)內(nèi)存的消耗非常大捡絮,很容易導(dǎo)致頁面出現(xiàn)卡幀的現(xiàn)象;
那么需要怎么避免這種情況呢莲镣,請(qǐng)看??????
1福稳、在工具類中定義一個(gè)方法,在需要處理圓形圖片的地方進(jìn)行調(diào)用
/**
* imageView添加圓形圖片
*
* @param Iv 添加圖片空間
* @param url 圖片URL
* @param image 站位image
*/
+(void)imageViewCircular:(nullable UIImageView *)Iv withUrl:(nullable NSString *)url placeholderImage:(nullable NSString *)image{
NSString *imageUrl = [NSString stringWithFormat:@"%@%@",TTPROTOCOL_IMAGE_URL,url];
[Iv sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:image ==nil ? nil :[UIImage imageNamed:image]completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
Iv.image=[image circleImage];
}];
}
2瑞侮、寫一個(gè)UIImage的分類(UIImage+Category )在分類中定義一個(gè)方法【-(nullable UIImage *)circleImage 】對(duì)image進(jìn)行處理
-(nullable UIImage *)circleImage{
//NO代表透明
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
//獲得上下文
CGContextRef ctx =UIGraphicsGetCurrentContext();
CGRect rect =CGRectMake(0, 0, self.size.width, self.size.height);
CGContextAddEllipseInRect(ctx, rect);
//裁剪
CGContextClip(ctx);
//將圖片畫上去
[self drawInRect:rect];
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
圖片的圓角處理的其他的方案的圆,歡迎補(bǔ)充,O(∩_∩)O謝謝半火!