UIColor与模式图像和颜色背后
我想设置一些UIView
元素的背景颜色。
我想用蓝色和上面的破折号图案来绘制它。 我知道我可以创build一个新的UIView作为它的孩子和绘画
[UIColor colorWithPatternImage:pattern]
但我会特别想要一个UIView
,所以得到一个具有蓝色背景和模式图像的UIColor
顶部。
马尔科
编辑:
模式图像是这样的:
结果应该是:
- (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; }
使用以上方法如下
UIImage *img = [UIImage imageNamed:@"Z2AmQ.png"]; [self.tmpView setBackgroundColor:[UIColor colorWithPatternImage:[self image:img withColor:[UIColor blueColor]]]];