1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
//添加水印
+ (UIImage *)getWaterMarkWith:(UIImage *)image waterText:(NSString *)str{
CGSize imageSize = image.size;
CGFloat imageWidth = imageSize.width;
CGFloat imageHeight = imageSize.height;
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
[image drawAtPoint:CGPointZero];
CGFloat textFont = 10 * ceilf(imageWidth / [UIScreen mainScreen].bounds.size.width);
CGSize perSize = [self getSize:imageWidth font:[UIFont systemFontOfSize:textFont] str:str lineSpace:0];
CGRect rect = CGRectMake(ceilf(imageWidth - perSize.width - 20),
ceilf(imageHeight - perSize.height - 20),
ceilf(perSize.width + 10),
ceilf(perSize.height + 5));
NSDictionary *colorInfo = [self mostColor:image scale:1 rect:rect];
CGFloat red = [colorInfo[@"red"] floatValue];
CGFloat green = [colorInfo[@"green"] floatValue];
CGFloat blue = [colorInfo[@"blue"] floatValue];
CGFloat alpha = [colorInfo[@"alpha"] floatValue];
BOOL colorDark = [self isDarkColor:[UIColor colorWithRed:red green:green blue:blue alpha:alpha]];
[str drawAtPoint:rect.origin withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:textFont], NSForegroundColorAttributeName:colorDark ? [UIColor whiteColor] : [UIColor blackColor]}];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
// 获取图片、区域的主色
+ (NSDictionary *)mostColor:(UIImage *)image scale:(CGFloat)scale rect:(CGRect)rect{
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
#else
int bitmapInfo = kCGImageAlphaPremultipliedLast;
#endif
if (scale <= 0.1) {
scale = 0.1;
}else if(scale >= 1){
scale = 1;
}
if (rect.size.width > 0 && rect.size.height > 0) {
image = [self cropSquareImage:image rect:rect];
}
//第一步 先把图片缩小 加快计算速度. 但越小结果误差可能越大
CGSize thumbSize = CGSizeMake([image size].width * scale, [image size].height * scale);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
thumbSize.width,
thumbSize.height,
8,//bits per component
thumbSize.width*4,
colorSpace,
bitmapInfo);
CGRect drawRect = CGRectMake(0, 0, thumbSize.width, thumbSize.height);
CGContextDrawImage(context, drawRect, image.CGImage);
CGColorSpaceRelease(colorSpace);
//第二步 取每个点的像素值
unsigned char* data = CGBitmapContextGetData (context);
if (data == NULL){
CGContextRelease(context);
return nil;
}
NSCountedSet *cls = [NSCountedSet setWithCapacity:thumbSize.width*thumbSize.height];
for (int x=0; x<thumbSize.height; x++) {
for (int y=0; y<thumbSize.width; y++) {
int offset = 4*(x*thumbSize.width + y);
int red = data[offset];
int green = data[offset+1];
int blue = data[offset+2];
int alpha = data[offset+3];
NSArray *clr=@[@(red),@(green),@(blue),@(alpha)];
[cls addObject:clr];
}
}
CGContextRelease(context);
//第三步 找到出现次数最多的那个颜色
NSEnumerator *enumerator = [cls objectEnumerator];
NSArray *curColor = nil;
NSArray *MaxColor=nil;
NSUInteger MaxCount=0;
while ( (curColor = [enumerator nextObject]) != nil ){
NSUInteger tmpCount = [cls countForObject:curColor];
if ( tmpCount < MaxCount ) continue;
MaxCount=tmpCount;
MaxColor=curColor;
}
//返回三色值+透明度
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
[dic setValue:@([MaxColor[0] intValue]/255.0f) forKey:@"red"];
[dic setValue:@([MaxColor[1] intValue]/255.0f) forKey:@"green"];
[dic setValue:@([MaxColor[2] intValue]/255.0f) forKey:@"blue"];
[dic setValue:@([MaxColor[3] intValue]/255.0f) forKey:@"alpha"];
return dic;
}
//裁剪图片
+ (UIImage *)cropSquareImage:(UIImage *)image rect:(CGRect)rect{
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
return newImage;
}
//判断颜色深浅
+ (BOOL)isDarkColor:(UIColor *)newColor{
const CGFloat *componentColors = CGColorGetComponents(newColor.CGColor);
CGFloat colorBrightness = ((componentColors[0] * 299) + (componentColors[1] * 587) + (componentColors[2] * 114)) / 1000;
if (colorBrightness < 0.5){
//深色
return YES;
} else{
//浅色
return NO;
}
}
|