我正在尝试在Swift中获取UIImage的主要颜色,并尝试移植此代码。不幸的是,该代码一直返回相同的颜色。我看到此处提供的答案也一直返回相同的颜色。在我的研究中,我避免使用CIFilter,因为它只返回平均颜色。
以下是移植的代码。我将CGContext数据设置为nil,因为Swift可以自行处理内存,在我的测试中它会产生许多内存错误。
以下是移植的代码。我将CGContext数据设置为nil,因为Swift可以自行处理内存,在我的测试中它会产生许多内存错误。
func mainColors(image:UIImage, detail: Int) -> [UIColor] {
//COLOR PROCESS STEP 1:
//Determine the detail.
var dimension = 10
var flexibility = 2
var range = 60
//Low detail.
if detail == 0 {
dimension = 4
flexibility = 1
range = 100
}
//High detail.
else if detail == 2 {
dimension = 100
flexibility = 10
range = 20
}
//COLOR PROCESS STEP 2:
//Determine the colors in the image.
//Create an array to store the colors.
var colors = Array<Array<CGFloat>>()
//Get the bitmap data of the image.
let imageRef = image.cgImage
//Variable to store the color space, RGB in this case.
let colorSpace = CGColorSpaceCreateDeviceRGB()
//Additional CGContext data.
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * dimension
let bitsPerComponent = 8
//Create the context. Data uses the memory pointer created above, the width and height determine the dimensions of the bitmap, the space is for the colorspace, the bitmap specifies the alpha channel.
let context = CGContext(data: nil, width: dimension, height: dimension, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue)!
//Draw the image.
let rect = CGRect(x: 0, y: 0, width: dimension, height: dimension)
context.draw(imageRef!, in: rect)
//Iterate through the raw data in order to create a UIColor.
var x = 0
var y = 0
for _ in 0..<(dimension * dimension) {
let index = (bytesPerRow * y) + x * bytesPerPixel
let red = CGFloat(index)
let green = CGFloat(index + 1)
let blue = CGFloat(index + 2)
let alpha = CGFloat(index + 3)
let color = [red, green, blue, alpha]
colors.append(color)
y += 1
if y == dimension {
y = 0
x += 1
}
}
//Deallocate the mutable pointer.
//free(rawData)
//COLOR PROCESS STEP 3:
//Add some color flexibility.
//Create an array containing the previous colored items and create another one for the flexible colors.
var copiedColors = colors
var flexibleColors = Array<String>()
//Iterate through the copied colors in order to create an improved UIColor.
let flexFactor = flexibility * 2 + 1
let factor = flexFactor * flexFactor * 3
for n in 0..<(dimension * dimension) {
let pixelColors = copiedColors[n]
var reds = Array<CGFloat>()
var greens = Array<CGFloat>()
var blues = Array<CGFloat>()
for p in 0..<3 {
let rgb = pixelColors[p]
for f in -flexibility...flexibility {
var newRGB = rgb + CGFloat(f)
if newRGB < 0 {
newRGB = 0
}
switch p {
case 0:
reds.append(newRGB)
case 1:
greens.append(newRGB)
case 2:
blues.append(newRGB)
default:
print("Error! Loop out of range! \(p)")
}
}
}
var r = 0
var g = 0
var b = 0
for _ in 0..<factor {
let red = reds[r]
let green = greens[g]
let blue = blues[b]
let rgbString = "\(red),\(green),\(blue)"
flexibleColors.append(rgbString)
b += 1
if b == flexFactor {
b = 0
g += 1
}
if g == flexFactor {
g = 0
r += 1
}
}
}
//COLOR PROCESS STEP 4:
//Distinguish the colors. Orders the flexible colors by their occurence and then keeps them if they are sufficiently disimilar.
//Dictionary to store all the colors.
let colorCounter = NSMutableDictionary()
//Check the number of times item is in array.
let countedSet = NSCountedSet(array: flexibleColors)
for item in countedSet {
let item = item as! String
let count = countedSet.count(for: item)
let value = NSNumber(integerLiteral: count)
colorCounter.setValue(value, forKey: item)
}
//Sort keys from highest occurence to lowest.
let orderedKeys = colorCounter.keysSortedByValue(comparator: {
(obj1, obj2) in
let x = obj1 as! NSNumber
let y = obj2 as! NSNumber
return x.compare(y)
})
//Check if the color is similar to another one already included.
var ranges = Array<String>()
for key in orderedKeys as! [String] {
let rgb = key.components(separatedBy: ",")
let r = NSString(string: rgb[0]).integerValue
let g = NSString(string: rgb[1]).integerValue
let b = NSString(string: rgb[2]).integerValue
var exclude = false
for rangedkey in ranges {
let rangedRGB = rangedkey.components(separatedBy: ",")
let ranged_r = NSString(string: rangedRGB[0]).integerValue
let ranged_g = NSString(string: rangedRGB[1]).integerValue
let ranged_b = NSString(string: rangedRGB[2]).integerValue
if r >= ranged_r - range && r <= ranged_r + range {
if g >= ranged_g - range && g <= ranged_g + range {
if b >= ranged_b - range && b <= ranged_b + range {
exclude = true
}
}
}
}
if exclude == false {
ranges.append(key)
}
}
//Create the colors and fill them.
var mainColors = Array<UIColor>()
for key in ranges {
let rgb = key.components(separatedBy: ",")
let r = NSString(string: rgb[0]).floatValue
let g = NSString(string: rgb[1]).floatValue
let b = NSString(string: rgb[2]).floatValue
let finalColor = UIColor(red: CGFloat((r / 255)), green: CGFloat((g / 255)), blue: CGFloat((b / 255)), alpha: CGFloat(1.0))
mainColors.append(finalColor)
}
return mainColors
}