[英]UIColor with pattern image and color behind
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
我知道我可以把一个新的UIView作为它的孩子并用它绘画
[UIColor colorWithPatternImage:pattern]
but I would specifically want one UIView
, so get one UIColor
with blue background and pattern image on top. 但我特别想要一个
UIView
,所以得到一个蓝色背景和图案图像在顶部的UIColor
。
Marko 马尔科
EDIT : 编辑:
Pattern image is like this : 图案图像是这样的:
And the result should be : 结果应该是:
- (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;
}
use above method as follows 使用上述方法如下
UIImage *img = [UIImage imageNamed:@"Z2AmQ.png"];
[self.tmpView setBackgroundColor:[UIColor colorWithPatternImage:[self image:img withColor:[UIColor blueColor]]]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.