Cocoa Touch - 使用叠加视图添加纹理 [英] Cocoa Touch - Adding texture with overlay view

查看:133
本文介绍了Cocoa Touch - 使用叠加视图添加纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组瓷砖作为UIViews具有可编程的背景颜色,每个
可以是不同的颜色。我想添加纹理,像侧面照明的斜面,每个。可以使用重叠视图或其他方法吗?

I have a set of tiles as UIViews that have a programmable background color, and each one can be a different color. I want to add texture, like a side-lit bevel, to each one. Can this be done with an overlay view or by some other method?

我正在寻找不需要每个案例的自定义图像文件的建议。

I'm looking for suggestions that don't require a custom image file for each case.

推荐答案

这可能有助于某人,虽然这是从SO的其他主题拼凑而成。
要为正常和视网膜显示创建任意颜色的斜角平铺图像,我在photoshop中创建了一个斜面图像,并将饱和度设置为零,使得一个灰度图像称为 tileBevel.png

This may help someone, although this was pieced together from other topics on SO. To create a beveled tile image with an arbitrary color for normal and for retina display, I made a beveled image in photoshop and set the saturation to zero, making a grayscale image called tileBevel.png

我还为视网膜显示器创建了一个( tileBevel@2x.png

I also created one for the retina display (tileBevel@2x.png)

这里是代码:

+ (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:@"tileBevel@2x.png"];        
    }
    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:方法所需的当前上下文。核心图形函数需要上下文作为参数,通过调用获得当前上下文。

This checks to see if the retina display is used, sizes the rectangle to draw in, picks the appropriate grayscale base image, set the blending mode to overlay, then draws a rectangle on top of the bottom image. All of this is done inside a graphics context bracketed by the BeginImageContext and EndImageContext calls. These set the current context needed by the UIImage drawRect: method. The Core Graphics functions need the context as a parameter, which is obtained by a call to get the current context.

结果如下所示:

>

这篇关于Cocoa Touch - 使用叠加视图添加纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆