开发中,可能会使用到圆角图片,如果直接设置ImageView的layer属性,会提前开启屏幕渲染到离屏渲染,消耗性能,通过UIImage分类的方式,实现创建纯色图片、创建带圆角的纯色图片、设置圆角图片以及生成带圆环的圆角图片四个方法
.h
#import <UIKit/UIKit.h>
@interface UIImage (Color)
/**
* 创建纯色图片
*
* @param color 生成纯色图片的颜色
* @param imageSize 需要创建纯色图片的尺寸
*
* @return 纯色图片
*/
+ (UIImage *)js_createImageWithColor:(UIColor *)color withSize:(CGSize)imageSize;
/**
* 创建圆角图片
*
* @param originalImage 原始图片
*
* @return 带圆角的图片
*/
+ (UIImage *)js_imageWithOriginalImage:(UIImage *)originalImage;
/**
* 创建圆角纯色图片
*
* @param color 设置圆角纯色图片的颜色
* @param imageSize 设置元角纯色图片的尺寸
*
* @return 圆角纯色图片
*/
+ (UIImage *)js_createRoundedImageWithColor:(UIColor *)color withSize:(CGSize)imageSize;
/**
* 生成带圆环的圆角图片
*
* @param originalImage 原始图片
* @param borderColor 圆环颜色
* @param borderWidth 圆环宽度
*
* @return 带圆环的圆角图片
*/
+ (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];
}
// 生成带圆环的圆角图片
+ (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