本文介绍了带有图案图像和颜色的UIColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试设置某些UIView
元素的背景颜色.
I am trying to set the background color of some UIView
element.
我想在上面涂上蓝色和虚线图案.我知道我可以将新的UIView作为其子级并用
I would like to paint it with blue color and a dash pattern on top.I know I could make a new UIView as its child and paint it with
[UIColor colorWithPatternImage:pattern]
但是我特别想要一个UIView
,所以得到一个带有蓝色背景和图案图像在顶部的UIColor
.
but I would specifically want one UIView
, so get one UIColor
with blue background and pattern image on top.
Marko
模式图像是这样的:
结果应为:
推荐答案
- (UIImage *)image:(UIImage *)image withColor:(UIColor *)color {
CGSize backgroundSize = image.size;
UIGraphicsBeginImageContext(backgroundSize);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect backgroundRect;
backgroundRect.size = backgroundSize;
backgroundRect.origin.x = 0;
backgroundRect.origin.y = 0;
float r,g,b,a;
[color getRed:&r green:&g blue:&b alpha:&a];
CGContextSetRGBFillColor(ctx, r, g, b, a);
CGContextFillRect(ctx, backgroundRect);
CGRect imageRect;
imageRect.size = image.size;
imageRect.origin.x = (backgroundSize.width - image.size.width)/2;
imageRect.origin.y = (backgroundSize.height - image.size.height)/2;
// Unflip the image
CGContextTranslateCTM(ctx, 0, backgroundSize.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextDrawImage(ctx, imageRect, image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
使用上述方法a
UIImage *img = [UIImage imageNamed:@"Z2AmQ.png"];
[self.tmpView setBackgroundColor:[UIColor colorWithPatternImage:[self image:img withColor:[UIColor blueColor]]]];
这篇关于带有图案图像和颜色的UIColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!