開發(fā)中,可能會使用到圓角圖片,如果直接設置ImageView的layer屬性,會提前開啟屏幕渲染到離屏渲染,消耗性能,通過UIImage分類的方式,實現(xiàn)創(chuàng)建純色圖片、創(chuàng)建帶圓角的純色圖片、設置圓角圖片以及生成帶圓環(huán)的圓角圖片四個方法
.h
#import <UIKit/UIKit.h>
@interface UIImage (Color)
/**
* 創(chuàng)建純色圖片
*
* @param color 生成純色圖片的顏色
* @param imageSize 需要創(chuàng)建純色圖片的尺寸
*
* @return 純色圖片
*/
+ (UIImage *)js_createImageWithColor:(UIColor *)color withSize:(CGSize)imageSize;
/**
* 創(chuàng)建圓角圖片
*
* @param originalImage 原始圖片
*
* @return 帶圓角的圖片
*/
+ (UIImage *)js_imageWithOriginalImage:(UIImage *)originalImage;
/**
* 創(chuàng)建圓角純色圖片
*
* @param color 設置圓角純色圖片的顏色
* @param imageSize 設置元角純色圖片的尺寸
*
* @return 圓角純色圖片
*/
+ (UIImage *)js_createRoundedImageWithColor:(UIColor *)color withSize:(CGSize)imageSize;
/**
* 生成帶圓環(huán)的圓角圖片
*
* @param originalImage 原始圖片
* @param borderColor 圓環(huán)顏色
* @param borderWidth 圓環(huán)寬度
*
* @return 帶圓環(huán)的圓角圖片
*/
+ (UIImage *)js_imageWithOriginalImage:(UIImage *)originalImage withBorderColor:(UIColor *)borderColor withBorderWidth:(CGFloat)borderWidth;
@end
.m
#import "UIImage+Color.h"
@implementation UIImage (Color)
// 生成純色圖片
+ (UIImage *)js_createImageWithColor:(UIColor *)color withSize:(CGSize)imageSize{
CGRect rect = CGRectMake(0.0f, 0.0f, imageSize.width, imageSize.height);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
// 生成圓角圖片
+ (UIImage *)js_imageWithOriginalImage:(UIImage *)originalImage{
CGRect rect = CGRectMake(0, 0, originalImage.size.width, originalImage.size.height);
UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, 0.0);
CGFloat cornerRadius = MIN(originalImage.size.width, originalImage.size.height) * 0.5;
[[UIBezierPath bezierPathWithRoundedRect:rect
cornerRadius:cornerRadius] addClip];
[originalImage drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
// 生成純色圓角圖片
+ (UIImage *)js_createRoundedImageWithColor:(UIColor *)color withSize:(CGSize)imageSize{
UIImage *originalImage = [self js_createImageWithColor:color withSize:imageSize];
return [self js_imageWithOriginalImage:originalImage];
}
// 生成帶圓環(huán)的圓角圖片
+ (UIImage *)js_imageWithOriginalImage:(UIImage *)originalImage withBorderColor:(UIColor *)borderColor withBorderWidth:(CGFloat)borderWidth{
CGRect rect = CGRectMake(0, 0, originalImage.size.width, originalImage.size.height);
UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, 0.0);
CGFloat cornerRadius = MIN(originalImage.size.width, originalImage.size.height) * 0.5;
[[UIBezierPath bezierPathWithRoundedRect:rect
cornerRadius:cornerRadius] addClip];
[originalImage drawInRect:rect];
CGPoint center = CGPointMake(originalImage.size.width * 0.5, originalImage.size.height * 0.5);
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:cornerRadius - borderWidth*0.5 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
circlePath.lineWidth = borderWidth;
[borderColor setStroke];
[circlePath stroke];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end