在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
想要实现一个功能,需要知道图片的主题色,网上搜了很多都是OC写的,Swift写的几乎没有,自己找了很多博客和文档,对UIImage做了extension,重写了一个mostColor的属性get方法。方便直接调用。有更好的办法希望大家多多分享,欢迎留言。为了方便大家,那我把OC写的也放在下边了
需要注明!!!!! 我写的这个swift版本计算主题色,为了追求精确度,计算了所有的,如果你觉得得到的结果很慢,一般主题色也不会要求很精确可以这样改: 在这个for循环的地方
改成 let offSet = x * y *4 就OK啦
Swift
extensionUIImage { var mostColor:UIColor { //获取图片信息 let imgWidth:Int =Int(self.size.width) /2 let imgHeight:Int =Int(self.size.height) /2
//位图的大小=图片宽*图片高*图片中每点包含的信息量 let bitmapByteCount = imgWidth * imgHeight *4
//使用系统的颜色空间 let colorSpace =CGColorSpaceCreateDeviceRGB()
//根据位图大小,申请内存空间 let bitmapData =malloc(bitmapByteCount) defer {free(bitmapData)}
//创建一个位图 let context =CGContext(data: bitmapData, width: imgWidth, height: imgHeight, bitsPerComponent:8, bytesPerRow: imgWidth *4, space: colorSpace, bitmapInfo:CGImageAlphaInfo.premultipliedLast.rawValue)
//图片的rect let rect =CGRect(x:0, y:0, width:CGFloat(imgWidth), height:CGFloat(imgHeight)) //将图片画到位图中 context?.draw(self.cgImage!, in: rect)
//获取位图数据 let bitData = context?.data let data =unsafeBitCast(bitData, to:UnsafePointer<CUnsignedChar>.self)
let cls =NSCountedSet.init(capacity: imgWidth * imgHeight) for xin0..<imgWidth { for yin0..<imgHeight { let offSet = (y * imgWidth + x) *4 let r = (data+ offSet ).pointee let g = (data+ offSet+1).pointee let b = (data+ offSet+2).pointee let a = (data+ offSet+3).pointee if a >0 { //去除透明 if!(r ==255&& g ==255&& b ==255) { //去除白色 cls.add([CGFloat(r),CGFloat(g),CGFloat(b),CGFloat(a)]) } }
} }
//找到出现次数最多的颜色 let enumerator = cls.objectEnumerator() var maxColor:Array<CGFloat>? =nil var maxCount =0 whilelet curColor = enumerator.nextObject() { let tmpCount = cls.count(for: curColor) if tmpCount >= maxCount{ maxCount = tmpCount maxColor = curColoras?Array<CGFloat> } } returnUIColor.init(red: (maxColor![0] /255), green: (maxColor![1] /255), blue: (maxColor![2] /255), alpha: (maxColor![3] /255)) }
}
OC
- (UIColor*)mainColor{
int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
//第一步先把图片缩小加快计算速度.但越小结果误差可能越大 CGSize thumbSize =CGSizeMake(self.size.width /2, self.size.height /2);
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,self.CGImage); CGColorSpaceRelease(colorSpace);
//第二步取每个点的像素值 unsigned char* data =CGBitmapContextGetData (context); if (data == NULL) return nil;
NSCountedSet *cls = [NSCountedSetsetWithCapacity:thumbSize.width * thumbSize.height];
for (int x =0; x < thumbSize.width; x++) { for (int y =0; y < thumbSize.height; y++) { // int offset = 4 * (x + y * thumbSize.width); int offset =4 * x * y; int red = data[offset]; int green = data[offset +1]; int blue = data[offset +2]; int alpha = data[offset +3]; if (alpha >0) { //去除透明 if (!(red ==255 && green ==255 && blue ==255)) { //去除白色 NSArray *clr =@[@(red),@(green),@(blue),@(alpha)]; [cls addObject:clr]; } } } } CGContextRelease(context);
//第三步找到出现次数最多的那个颜色 NSEnumerator *enumerator = [clsobjectEnumerator]; NSArray *curColor =nil; NSArray *MaxColor =nil; NSUInteger MaxCount =0; while ( (curColor = [enumeratornextObject]) !=nil ) { NSUInteger tmpCount = [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)]; }
|
请发表评论