開(kāi)發(fā)中,可能會(huì)使用到圓角圖片,如果直接設(shè)置ImageView的layer屬性,會(huì)提前開(kāi)啟屏幕渲染到離屏渲染,消耗性能,通過(guò)UIImage分類(lèi)的方式,實(shí)現(xiàn)創(chuàng)建純色圖片萝挤、創(chuàng)建帶圓角的純色圖片御毅、設(shè)置圓角圖片以及生成帶圓環(huán)的圓角圖片四個(gè)方法
.h
#import <UIKit/UIKit.h>
@interface UIImage (Color)
/**
* 創(chuàng)建純色圖片
*
* @param color 生成純色圖片的顏色
* @param imageSize 需要?jiǎng)?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 設(shè)置圓角純色圖片的顏色
* @param imageSize 設(shè)置元角純色圖片的尺寸
*
* @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