ios - UIColor 图案图像和色调颜色

标签 ios uiimage uicolor tintcolor

我正在开发一个应用程序,其中的元素需要以不同的颜色进行自定义。到目前为止,我一直在利用 tintColorimageWithRenderingMode 来更改图像颜色。我目前处于需要使用 UIColor(patternImage:)backgroundColor 添加背景图 block 图像的情况。有没有办法对背景图 block 图像应用一种色调,以便我可以在运行时更改背景图像颜色?

最佳答案

J_P,谢谢你的回答!这真的很有帮助。我只想发布 Swift 5 的更新版本

extension UIColor {
    convenience init(patternImage: UIImage, tintColor: UIColor) {
        var image = patternImage.withRenderingMode(.alwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(patternImage.size,
                                               false,
                                               patternImage.scale)
        tintColor.set()   
        image.draw(in: CGRect(x: 0, y: 0,
                              width: patternImage.size.width,
                              height: patternImage.size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        self.init(patternImage: image)
    }
}

关于ios - UIColor 图案图像和色调颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31957187/

相关文章:

javascript - React Native 中应用被杀死后存储数据的数据库

swift - Swift 中的 CIDetector 透视和裁剪

iphone - 如何将 2 张图像合并/合并为 1 张

swift - Swift 错误中的 UIColor 扩展

ios - 使用 UIColor 平铺图像作为 View 的背景不适用于cornerRadius

objective-c - - (id)copyWithZone :(NSZone *)zone - memory leaks

ios - IOS 版本兼容的本地和推送通知

ios - 将Apple开发人员帐户和证书转移到新计算机

objective-c - 在 UITableView 中的 UIImageView 中从网络加载图像

ios - TTAtributedLabel 设置多色使用 UIColor,它在 header 中用 MACRO 定义?