ios UIColor图案图像和色调颜色

llew8vvj  于 2023-03-05  发布在  iOS
关注(0)|答案(4)|浏览(195)

我正在开发一个应用程序,其中的元素需要定制为不同的颜色。到目前为止,我一直在利用tintColorimageWithRenderingMode来改变图像颜色。我目前的情况是,我需要使用UIColor(patternImage:)backgroundColor添加一个背景瓷砖图像。是否有一种方法可以将一种色调应用于背景平铺图像,以便我可以在运行时更改背景图像颜色?

ekqde3dh

ekqde3dh1#

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)
    }
}
okxuctiv

okxuctiv2#

您可以将TintColor应用于UIImage并将该UIImage用作平铺的背景色。
对于将tintColor应用于UIImage:

- (UIImage *) addOverlaytoImage:(UIImage *)mySourceImage
{
    UIImage * image = mySourceImage;
    UIColor * color = [UIColor yellowColor];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height) blendMode:kCGBlendModeNormal alpha:1];
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    [color setFill];
    [path fillWithBlendMode:kCGBlendModeMultiply alpha:1]; //look up blending modes for your needs
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

然后将此图像添加到背景色参数:

yourView.backgroundColor = [UIColor colorWithPatternImage:[self addOverlaytoImage:myImage]];

请参考以下链接:Designing for iOS: Blending Modes

hec6srdp

hec6srdp3#

与链接,我发现here这是我得到它的工作

// originalImage and originalColor are defined
var image = originalImage.imageWithRenderingMode(.AlwaysTemplate)
UIGraphicsBeginImageContextWithOptions(
    originalImage.size,
    false,
    originalImage.scale)
originalColor.set()
image.drawInRect(CGRectMake(
    0,
    0,
    originalImage.size.width,
    originalImage.size.height))
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return UIColor(patternImage: image)
mwg9r5ms

mwg9r5ms4#

另一个支持亮/暗模式的动态tintColor版本:

extension UIColor {
    convenience init(patternImage: UIImage, tintColor: UIColor) {
        self.init(dynamicProvider: { traitCollection in
            var staticColor: UIColor = .clear

            traitCollection.performAsCurrent {
                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() ?? UIImage()
                UIGraphicsEndImageContext()

                staticColor = UIColor(patternImage: image)
            }

            return staticColor
        })
    }
}

相关问题