UIImage+Exension.h
#import <UIKit/UIKit.h>
@interface UIImage (Exension)
/**
生成一张高斯模糊的图片
@param image 原图
@param blur 模糊程度
@return 高斯模糊的图片
*/
+ (UIImage *)blurImage:(UIImage *)image blur:(CGFloat)blur;
/**
根据颜色生成一张图片
@param color 颜色
@param size 要生成图片的尺寸
@return 图片
*/
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;
/**
生成一张圆形图片
@param originImage 原始图片
@param borderColor 边框颜色
@param borderWidth 边框宽度
@return 圆形图片
*/
+ (UIImage *)circleImage:(UIImage *)originImage borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth;
/**
生成一张不被渲染的图片
@param imageName 图片名称
@return 不会被渲染的原始图片
*/
+ (UIImage *)imageRenderingModeAlowsOriginal:(NSString *)imageName;
@end
UIImage+Exension.m
#import "UIImage+Exension.h"
#import <Accelerate/Accelerate.h>
@implementation UIImage (Exension)
/**
生成一张高斯模糊的图片
*/
+ (UIImage *)blurImage:(UIImage *)image blur:(CGFloat)blur
{
// 模糊度越界
if (blur < 0.f || blur > 1.f) {
blur = 0.5f;
}
int boxSize = (int)(blur * 40);
boxSize = boxSize - (boxSize % 2) + 1;
CGImageRef img = image.CGImage;
vImage_Buffer inBuffer, outBuffer;
vImage_Error error;
void *pixelBuffer;
//从CGImage中获取数据
CGDataProviderRef inProvider = CGImageGetDataProvider(img);
CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
//设置从CGImage获取对象的属性
inBuffer.width = CGImageGetWidth(img);
inBuffer.height = CGImageGetHeight(img);
inBuffer.rowBytes = CGImageGetBytesPerRow(img);
if (inBitmapData) {
inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
}
pixelBuffer = malloc(CGImageGetBytesPerRow(img) *
CGImageGetHeight(img));
if(pixelBuffer == NULL)
NSLog(@"No pixelbuffer");
outBuffer.data = pixelBuffer;
outBuffer.width = CGImageGetWidth(img);
outBuffer.height = CGImageGetHeight(img);
outBuffer.rowBytes = CGImageGetBytesPerRow(img);
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
if (error) {
NSLog(@"error from convolution %ld", error);
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(
outBuffer.data,
outBuffer.width,
outBuffer.height,
8,
outBuffer.rowBytes,
colorSpace,
kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
CFRelease(inBitmapData);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return returnImage;
}
/**
根据颜色生成一张图片
*/
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
if (color) {
CGRect rect = CGRectMake(0, 0, size.width, size.height);
// 开启图形上下文
UIGraphicsBeginImageContext(rect.size);
// 获取当前的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 将颜色填充到上下文
CGContextSetFillColorWithColor(context, color.CGColor);
// 将内容填满指定的尺寸
CGContextFillRect(context, rect);
// 从上下文获取图片
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
return image;
}
return nil;
}
/**
生成一张圆形图片
*/
+ (UIImage *)circleImage:(UIImage *)originImage borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth
{
// 设置圆形图片的直径
CGFloat imageDia = originImage.size.width;
// 计算外圆的直径(边框是在图片外额外添加的区域)
CGFloat borderDia = imageDia + 2 * borderWidth;
// 开启图形上下文
UIGraphicsBeginImageContext(originImage.size);
// 画一个包含边框的圆形
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, borderDia, borderDia)];
// 设置颜色
[borderColor set];
[path fill];
// 设置裁剪区域
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderWidth, borderWidth, imageDia, imageDia)];
// 裁剪图片
[clipPath addClip];
// 绘制图片
[originImage drawAtPoint:CGPointMake(borderWidth, borderWidth)];
// 从上下文中获取图片
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
return resultImage;
}
/**
生成一张不被渲染的图片
*/
+ (UIImage *)imageRenderingModeAlowsOriginal:(NSString *)imageName
{
UIImage *image = [UIImage imageNamed:imageName];
return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
@end