1、首先加一个UIColor的分类。
#import "UIColor+mostColor.h"
2、获取图片主色调。
#import
@interfaceUIColor (mostColor)
/**
* 获取图片的主色调
*/
+ (UIColor*)mostColor:(UIImage*)image;
/**
* 获取图片某一区域的主色调
* @paramrect 获取色调的图片区域
*/
+ (UIColor*)mostColor:(UIImage*)image inRect:(CGRect)rect;
@end
#import "UIColor+mostColor.h"
@implementationUIColor (mostColor)
//获取图片特定区域的主色调
+ (UIColor*)mostColor:(UIImage*)image inRect:(CGRect)rect {
//首先缩放一下图片变成W375、H113大小的图片
UIImage*newImage = [selfscaleToSize:imagesize:CGSizeMake(375.0,113.0)];
if(newImage) {
//截取左下角,大小为rect方块的图片
UIImage*smallImage = [selfimageFromImage:newImageinRect:rect];
if(smallImage) {
UIColor*color = [selfmostColor:smallImage];
returncolor;
}
}
return nil;
}
/**
* 缩放图片
* @paramimg image
* @paramsize 缩放后的大小
* @returnimage
*/
+ (UIImage*)scaleToSize:(UIImage*)img size:(CGSize)size{
// 创建一个bitmap的context
CGFloatwidth =CGImageGetWidth(img.CGImage);
CGFloatheight =CGImageGetHeight(img.CGImage);
CGFloatmax = width >= height ? width:height;
CGSizeoriginSize;
if(max <=0) {
returnnil;
}
if(width >= height) {
originSize =CGSizeMake(size.width, (size.width* height)/width);
}else{
originSize =CGSizeMake((size.height* width)/height, size.height);
}
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[imgdrawInRect:CGRectMake((size.width- originSize.width)/2, (size.height- originSize.height)/2, originSize.width, originSize.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
returnscaledImage;
}
// 裁剪图片
+ (UIImage*)imageFromImage:(UIImage*)image inRect:(CGRect)rect {
CGImageRefsourceImageRef = [imageCGImage];
CGImageRefnewImageRef =CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage*newImage = [UIImageimageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
returnnewImage;
}
//根据图片获取图片的主色调
+ (UIColor*)mostColor:(UIImage*)image{
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
#else
intbitmapInfo = kCGImageAlphaPremultipliedLast;
#endif
//第一步 先把图片缩小 加快计算速度. 但越小结果误差可能越大
CGSizethumbSize=CGSizeMake(image.size.width/2, image.size.height/2);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
thumbSize.width,
thumbSize.height,
8,//bits per component
thumbSize.width*4,
colorSpace,
bitmapInfo);
CGRectdrawRect =CGRectMake(0,0, thumbSize.width, thumbSize.height);
CGContextDrawImage(context, drawRect, image.CGImage);
CGColorSpaceRelease(colorSpace);
//第二步 取每个点的像素值
unsignedchar* data =CGBitmapContextGetData(context);
if(data ==NULL)returnnil;
NSCountedSet*cls=[NSCountedSetsetWithCapacity:thumbSize.width*thumbSize.height];
for(intx=0; x
for(inty=0; y
intoffset =4*(x*y);
intred = data[offset];
intgreen = data[offset+1];
intblue = data[offset+2];
intalpha = data[offset+3];
if(alpha>0) {//去除透明
if(red==255&&green==255&&blue==255) {//去除白色
}else{
NSArray*clr=@[@(red),@(green),@(blue),@(alpha)];
[clsaddObject:clr];
}
}
}
}
CGContextRelease(context);
//第三步 找到出现次数最多的那个颜色
NSEnumerator*enumerator = [clsobjectEnumerator];
NSArray*curColor =nil;
NSArray*MaxColor=nil;
NSUIntegerMaxCount=0;
while( (curColor = [enumeratornextObject]) !=nil)
{
NSUIntegertmpCount = [clscountForObject:curColor];
if( tmpCount < MaxCount )continue;
MaxCount=tmpCount;
MaxColor=curColor;
}
return[UIColorcolorWithRed:([MaxColor[0]intValue]/255.0f)green:([MaxColor[1]intValue]/255.0f)blue:([MaxColor[2]intValue]/255.0f)alpha:([MaxColor[3]intValue]/255.0f)];
}
@end
网友评论