我有一组作为 UIView 的图块,它们具有可编程的背景颜色,并且每个图块
可以是不同的颜色。我想为每一个添加纹理,比如侧光斜角。这可以通过覆盖视图或其他方法来完成吗?
我正在寻找不需要为每种情况提供自定义图像文件的建议。
这可能会对某人有所帮助,尽管这是从其他主题中拼凑而成的。
为了创建一个具有任意颜色的斜面平铺图像以用于正常和视网膜显示,我在 Photoshop 中制作了一个斜面图像并将饱和度设置为零,制作了一个名为tileBevel.png
我还为视网膜显示器创建了一个([email protected] /cdn-cgi/l/email-protection
)
这是代码:
+ (UIImage*) createTileWithColor:(UIColor*)tileColor {
int pixelsHigh = 44;
int pixelsWide = 46;
UIImage *bottomImage;
if([UIScreen respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0) {
pixelsHigh *= 2;
pixelsWide *= 2;
bottomImage = [UIImage imageNamed:@"[email protected] /cdn-cgi/l/email-protection"];
}
else {
bottomImage = [UIImage imageNamed:@"tileBevel.png"];
}
CGImageRef theCGImage = NULL;
CGContextRef tileBitmapContext = NULL;
CGRect rectangle = CGRectMake(0,0,pixelsWide,pixelsHigh);
UIGraphicsBeginImageContext(rectangle.size);
[bottomImage drawInRect:rectangle];
tileBitmapContext = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(tileBitmapContext, kCGBlendModeOverlay);
CGContextSetFillColorWithColor(tileBitmapContext, tileColor.CGColor);
CGContextFillRect(tileBitmapContext, rectangle);
theCGImage=CGBitmapContextCreateImage(tileBitmapContext);
UIGraphicsEndImageContext();
return [UIImage imageWithCGImage:theCGImage];
}
这会检查是否使用视网膜显示器,调整要绘制的矩形的大小,选择适当的灰度基础图像,将混合模式设置为叠加,然后在底部图像的顶部绘制一个矩形。所有这些都是在由 BeginImageContext 和 EndImageContext 调用括起来的图形上下文中完成的。这些设置了 UIImage drawRect: 方法所需的当前上下文。 Core Graphics 函数需要上下文作为参数,该参数是通过调用获取当前上下文来获取的。
结果如下:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)