jjzjj

ios - 在 Swift 中获取 UIImage 的主要颜色

coder 2024-01-29 原文

我试图在 Swift 中获取 UIImage 的主要颜色,并尝试移植 this code .不幸的是,代码一直返回相同的颜色。我看到了the answer provided here也不断返回相同的颜色。我避免使用 CIFilter,因为它只返回我研究中的平均颜色。

在移植的代码下方。我已将 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
    }

最佳答案

这个很棒的开源类似乎运行良好:https://github.com/jathu/UIImageColors

将下面的此类导入到您的项目中。

//
//  UIImageColors.swift
//  https://github.com/jathu/UIImageColors
//
//  Created by Jathu Satkunarajah (@jathu) on 2015-06-11 - Toronto
//

import UIKit

public struct UIImageColors {
    public var background: UIColor!
    public var primary: UIColor!
    public var secondary: UIColor!
    public var detail: UIColor!

    public init(background: UIColor, primary: UIColor, secondary: UIColor, detail: UIColor) {
      self.background = background
      self.primary = primary
      self.secondary = secondary
      self.detail = detail
    }
}

public enum UIImageColorsQuality: CGFloat {
    case lowest = 50 // 50px
    case low = 100 // 100px
    case high = 250 // 250px
    case highest = 0 // No scale
}

fileprivate struct UIImageColorsCounter {
    let color: Double
    let count: Int
    init(color: Double, count: Int) {
        self.color = color
        self.count = count
    }
}

/*
    Extension on double that replicates UIColor methods. We DO NOT want these
    exposed outside of the library because they don't make sense outside of the
    context of UIImageColors.
*/
fileprivate extension Double {

    private var r: Double {
        return fmod(floor(self/1000000),1000000)
    }

    private var g: Double {
        return fmod(floor(self/1000),1000)
    }

    private var b: Double {
        return fmod(self,1000)
    }

    fileprivate var isDarkColor: Bool {
        return (r*0.2126) + (g*0.7152) + (b*0.0722) < 127.5
    }

    fileprivate var isBlackOrWhite: Bool {
        return (r > 232 && g > 232 && b > 232) || (r < 23 && g < 23 && b < 23)
    }

    fileprivate func isDistinct(_ other: Double) -> Bool {
        let _r = self.r
        let _g = self.g
        let _b = self.b
        let o_r = other.r
        let o_g = other.g
        let o_b = other.b

        return (fabs(_r-o_r) > 63.75 || fabs(_g-o_g) > 63.75 || fabs(_b-o_b) > 63.75)
            && !(fabs(_r-_g) < 7.65 && fabs(_r-_b) < 7.65 && fabs(o_r-o_g) < 7.65 && fabs(o_r-o_b) < 7.65)
    }

    fileprivate func with(minSaturation: Double) -> Double {
        // Ref: https://en.wikipedia.org/wiki/HSL_and_HSV

        // Convert RGB to HSV

        let _r = r/255
        let _g = g/255
        let _b = b/255
        var H, S, V: Double
        let M = fmax(_r,fmax(_g, _b))
        var C = M-fmin(_r,fmin(_g, _b))

        V = M
        S = V == 0 ? 0:C/V

        if minSaturation <= S {
            return self
        }

        if C == 0 {
            H = 0
        } else if _r == M {
            H = fmod((_g-_b)/C, 6)
        } else if _g == M {
            H = 2+((_b-_r)/C)
        } else {
            H = 4+((_r-_g)/C)
        }

        if H < 0 {
            H += 6
        }

        // Back to RGB

        C = V*minSaturation
        let X = C*(1-fabs(fmod(H,2)-1))
        var R, G, B: Double

        switch H {
        case 0...1:
            R = C
            G = X
            B = 0
        case 1...2:
            R = X
            G = C
            B = 0
        case 2...3:
            R = 0
            G = C
            B = X
        case 3...4:
            R = 0
            G = X
            B = C
        case 4...5:
            R = X
            G = 0
            B = C
        case 5..<6:
            R = C
            G = 0
            B = X
        default:
            R = 0
            G = 0
            B = 0
        }

        let m = V-C

        return (floor((R + m)*255)*1000000)+(floor((G + m)*255)*1000)+floor((B + m)*255)
    }

    fileprivate func isContrasting(_ color: Double) -> Bool {
        let bgLum = (0.2126*r)+(0.7152*g)+(0.0722*b)+12.75
        let fgLum = (0.2126*color.r)+(0.7152*color.g)+(0.0722*color.b)+12.75
        if bgLum > fgLum {
            return 1.6 < bgLum/fgLum
        } else {
            return 1.6 < fgLum/bgLum
        }
    }

    fileprivate var uicolor: UIColor {
        return UIColor(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: 1)
    }

    fileprivate var pretty: String {
        return "\(Int(self.r)), \(Int(self.g)), \(Int(self.b))"
    }
}

extension UIImage {
    private func resizeForUIImageColors(newSize: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
        defer {
            UIGraphicsEndImageContext()
        }
        self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        guard let result = UIGraphicsGetImageFromCurrentImageContext() else {
            fatalError("UIImageColors.resizeForUIImageColors failed: UIGraphicsGetImageFromCurrentImageContext returned nil.")
        }

        return result
    }


    public func getColors(quality: UIImageColorsQuality = .high, _ completion: @escaping (UIImageColors) -> Void) {
        DispatchQueue.global().async {
            let result = self.getColors(quality: quality)
            DispatchQueue.main.async {
                completion(result)
            }
        }
    }


    public func getColors(quality: UIImageColorsQuality = .high) -> UIImageColors {
        var scaleDownSize: CGSize = self.size
        if quality != .highest {
            if self.size.width < self.size.height {
                let ratio = self.size.height/self.size.width
                scaleDownSize = CGSize(width: quality.rawValue/ratio, height: quality.rawValue)
            } else {
                let ratio = self.size.width/self.size.height
                scaleDownSize = CGSize(width: quality.rawValue, height: quality.rawValue/ratio)
            }
        }

        let cgImage = self.resizeForUIImageColors(newSize: scaleDownSize).cgImage!
        let width: Int = cgImage.width
        let height: Int = cgImage.height

        let threshold = Int(CGFloat(height)*0.01)
        var proposed: [Double] = [-1,-1,-1,-1]

        guard let data = CFDataGetBytePtr(cgImage.dataProvider!.data) else {
            fatalError("UIImageColors.getColors failed: could not get cgImage data.")
        }

        let imageColors = NSCountedSet(capacity: width*height)
        for x in 0..<width {
            for y in 0..<height {
                let pixel: Int = ((width * y) + x) * 4
                if 127 <= data[pixel+3] {
                    imageColors.add((Double(data[pixel+2])*1000000)+(Double(data[pixel+1])*1000)+(Double(data[pixel])))
                }
            }
        }

        let sortedColorComparator: Comparator = { (main, other) -> ComparisonResult in
            let m = main as! UIImageColorsCounter, o = other as! UIImageColorsCounter
            if m.count < o.count {
                return .orderedDescending
            } else if m.count == o.count {
                return .orderedSame
            } else {
                return .orderedAscending
            }
        }

        var enumerator = imageColors.objectEnumerator()
        var sortedColors = NSMutableArray(capacity: imageColors.count)
        while let K = enumerator.nextObject() as? Double {
            let C = imageColors.count(for: K)
            if threshold < C {
                sortedColors.add(UIImageColorsCounter(color: K, count: C))
            }
        }
        sortedColors.sort(comparator: sortedColorComparator)

        var proposedEdgeColor: UIImageColorsCounter
        if 0 < sortedColors.count {
            proposedEdgeColor = sortedColors.object(at: 0) as! UIImageColorsCounter
        } else {
            proposedEdgeColor = UIImageColorsCounter(color: 0, count: 1)
        }

        if proposedEdgeColor.color.isBlackOrWhite && 0 < sortedColors.count {
            for i in 1..<sortedColors.count {
                let nextProposedEdgeColor = sortedColors.object(at: i) as! UIImageColorsCounter
                if Double(nextProposedEdgeColor.count)/Double(proposedEdgeColor.count) > 0.3 {
                    if !nextProposedEdgeColor.color.isBlackOrWhite {
                        proposedEdgeColor = nextProposedEdgeColor
                        break
                    }
                } else {
                    break
                }
            }
        }
        proposed[0] = proposedEdgeColor.color

        enumerator = imageColors.objectEnumerator()
        sortedColors.removeAllObjects()
        sortedColors = NSMutableArray(capacity: imageColors.count)
        let findDarkTextColor = !proposed[0].isDarkColor

        while var K = enumerator.nextObject() as? Double {
            K = K.with(minSaturation: 0.15)
            if K.isDarkColor == findDarkTextColor {
                let C = imageColors.count(for: K)
                sortedColors.add(UIImageColorsCounter(color: K, count: C))
            }
        }
        sortedColors.sort(comparator: sortedColorComparator)

        for color in sortedColors {
            let color = (color as! UIImageColorsCounter).color

            if proposed[1] == -1 {
                if color.isContrasting(proposed[0]) {
                    proposed[1] = color
                }
            } else if proposed[2] == -1 {
                if !color.isContrasting(proposed[0]) || !proposed[1].isDistinct(color) {
                    continue
                }
                proposed[2] = color
            } else if proposed[3] == -1 {
                if !color.isContrasting(proposed[0]) || !proposed[2].isDistinct(color) || !proposed[1].isDistinct(color) {
                    continue
                }
                proposed[3] = color
                break
            }
        }

        let isDarkBackground = proposed[0].isDarkColor
        for i in 1...3 {
            if proposed[i] == -1 {
                proposed[i] = isDarkBackground ? 255255255:0
            }
        }

        return UIImageColors(
            background: proposed[0].uicolor,
            primary: proposed[1].uicolor,
            secondary: proposed[2].uicolor,
            detail: proposed[3].uicolor
        )
    }
}

关于ios - 在 Swift 中获取 UIImage 的主要颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41552544/

有关ios - 在 Swift 中获取 UIImage 的主要颜色的更多相关文章

  1. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  2. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  3. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  4. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  5. ruby - 从 Ruby 中的主机名获取 IP 地址 - 2

    我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge

  6. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  7. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

  8. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  9. Ruby 从大范围中获取第 n 个项目 - 2

    假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit

  10. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

随机推荐