简体   繁体   English

UIColor图案图像和色调

[英]UIColor Pattern Image and Tint Color

I am working on an app where elements need to be customizable in different colors. 我正在开发一个应用程序,其中元素需要以不同的颜色进行自定义。 Up to this point I have been taking advantage of tintColor and imageWithRenderingMode to change image colors. 到目前为止,我一直在利用tintColorimageWithRenderingMode来改变图像颜色。 I am currently in a situation where I need to add a background tile image using UIColor(patternImage:) and backgroundColor . 我目前处于需要使用UIColor(patternImage:)backgroundColor添加背景图块图像的情况。 Is there a way to apply a kind of tint to the background tile image so I can change the background image color at runtime? 有没有办法将一种色调应用于背景图块图像,以便我可以在运行时更改背景图像颜色?

You can apply TintColor to UIImage and Use that UIImage as a background color for tiling. 您可以将TintColor应用于UIImage,并将该UIImage用作平铺的背景颜色。

For Applying tintColor to 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;
}

And then add this image to Background Color parameter: 然后将此图像添加到Background Color参数:

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

Please refer following link: Designing for iOS: Blending Modes 请参考以下链接: iOS设计:混合模式

With the link that I found here this is what I got it to work 通过我在这里找到的链接,这就是我的工作

// 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)

J_P, thanks for your answer! J_P,谢谢你的回答! It's really helpful. 这真的很有帮助。 I just want to post an updated version for Swift 5 我只想发布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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM