// 获取图片的主题色
public extension UIImage {
func subjectColor(_ completion: @escaping (_ color: UIColor?) -> Void) {
DispatchQueue.global().async {
// 获取不到cgImage时
if self.cgImage == nil {
DispatchQueue.main.async {
return completion(nil)
}
}
// 先缩小图片
let bitmapInfo = CGBitmapInfo(rawValue: 0).rawValue | CGImageAlphaInfo.premultipliedLast.rawValue
// 不要压缩太小,不然误差会太大
let thumbSize = CGSize(width: 40, height: 40)
let colorSpace = CGColorSpaceCreateDeviceRGB()
guard let context = CGContext(data: nil, width: Int(thumbSize.width), height: Int(thumbSize.height), bitsPerComponent: 8, bytesPerRow: Int(thumbSize.width) * 4, space: colorSpace, bitmapInfo: bitmapInfo) else { return completion(nil) }
let drawRect = CGRect(x: 0, y: 0, width: thumbSize.width, height: thumbSize.height)
context.draw(self.cgImage!, in: drawRect)
// 取每个点的像素值
if context.data == nil {
return completion(nil)
}
let countedSet = NSCountedSet(capacity: Int(thumbSize.width * thumbSize.height))
for x in 0 ..< Int(thumbSize.width) {
for y in 0 ..< Int(thumbSize.height) {
let offset = 4 * x * y
let red = context.data!.load(fromByteOffset: offset, as: UInt8.self)
let green = context.data!.load(fromByteOffset: offset+1, as: UInt8.self)
let blue = context.data!.load(fromByteOffset: offset+2, as: UInt8.self)
let alpha = context.data!.load(fromByteOffset: offset+3, as: UInt8.self)
// 过滤掉透明的,基本白色的,基本黑色的
if alpha > 0 && (red < 250 && blue < 250 && green < 250) && (red > 5 && green > 5 && blue > 5) {
let array = (red,green,blue,alpha)
countedSet.add(array)
}
}
}
// 找到出现次数最多的颜色
let enmurator = countedSet.objectEnumerator()
var maxColor: [Int] = []
var maxCount = 0
while let currentColor = enmurator.nextObject() as? [Int],!currentColor.isEmpty {
let tmpCount = countedSet.count(for: currentColor)
if tmpCount < maxCount {
continue
}
maxCount = tmpCount
maxColor = currentColor
}
let color = UIColor(red: CGFloat(maxColor[0]) / 255.0, green: CGFloat(maxColor[1]) / 255.0, blue: CGFloat(maxColor[2]) / 255.0, alpha: CGFloat(maxColor[3]) / 255.0)
DispatchQueue.main.async {
return completion(color)
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)