Swift platform: iOS, tvOS and macOS

UIImageColors

适用于 UIImageNSImage 的 iTunes 风格颜色提取器。 它提取最主要和最突出的颜色。

preview

安装

手动

UIImageColors.swift 复制到你的项目中。

Cocoapods

将 UIImageColors 添加到你的 Podfile

pod 'UIImageColors'

Carthage

将 UIImageColors 添加到你的 Cartfile

github "jathu/UIImageColors"

示例

异步示例

let image = UIImage(named: "yeezus.png")

image.getColors { colors in
  backgroundView.backgroundColor = colors.background
  mainLabel.textColor = colors.primary
  secondaryLabel.textColor = colors.secondary
  detailLabel.textColor = colors.detail
}

同步示例

let colors = UIImage(named: "yeezus.png").getColors()

backgroundView.backgroundColor = colors.background
mainLabel.textColor = colors.primary
secondaryLabel.textColor = colors.secondary
detailLabel.textColor = colors.detail

图像方法

getColors() -> UIImageColors?
getColors(quality: ImageColorsQuality) -> UIImageColors?
getColors(_ completion: (UIImageColors?) -> Void) -> Void
getColors(quality: UIImageColorsQuality, _ completion: (UIImageColors?) -> Void) -> Void

UIImageColors 对象

UIImageColors 是一个结构体,包含四个不同的 UIColor (或 macOS 上的 NSColor) 变量。

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

UIImageColorsQuality 是一个枚举,包含四个不同的质量选项。 这些质量选项决定原始图像被缩小的程度。 Lowest 意味着更小的尺寸和更快的性能,但牺牲了颜色质量。 High 意味着更大的尺寸和更慢的性能,但具有良好的颜色。 Highest 意味着不进行缩小,并具有非常好的颜色,但速度非常慢。

默认设置为 high

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

许可证

许可证 在项目文件夹中提供。 这是基于 Panic 的 OS X ColorArt


2015 年 6 月 - 多伦多