如何平铺CALayer的内容? [英] How to tile the contents of a CALayer?

查看:166
本文介绍了如何平铺CALayer的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



示例代码:

$ b $

我想在CALayer b

  self.background = [UIImage imageNamed:@Background.png]; 
[documentDisplay.layer setContents:self.background.CGImage];

如果可以,我想避免子类化。

解决方案

您可以使用图案图片创建颜色,并将其设置为图层的背景颜色。



你需要将 CGImage 包装在 CGPattern 中,并创建一个 CGColor 从它。

  //回调CreateImagePattern。 
static void drawPatternImage(void * info,CGContextRef ctx)
{
CGImageRef image =(CGImageRef)info;
CGContextDrawImage(ctx,
CGRectMake(0,0,CGImageGetWidth(image),CGImageGetHeight(image)),
image);
}

// CreateImagePattern的回调。
static void releasePatternImage(void * info)
{
CGImageRelease((CGImageRef)info);
}

//假设图像是你想要分配为图层背景的CGImage
int width = CGImageGetWidth(image);
int height = CGImageGetHeight(image);
static const CGPatternCallbacks callbacks = {0,& drawPatternImage,& releasePatternImage};
CGPatternRef pattern = CGPatternCreate(image,
CGRectMake(0,0,width,height),
CGAffineTransformMake(1,0,0,1,0,0),
width ,
height,
kCGPatternTilingConstantSpacing,
true,
& callbacks);
CGColorSpaceRef space = CGColorSpaceCreatePattern(NULL);
CGFloat components [1] = {1.0};
CGColorRef color = CGColorCreateWithPattern(space,pattern,components);
CGColorSpaceRelease(space);
CGPatternRelease(pattern);
yourLayer.backgroundColor = color; //将图层的背景设置为图片
CGColorRelease(color);

此代码是从GeekGameBoard示例代码修改的。


I would like to tile a CGImage across a CALayer, but it seems to only want to stretch it.

The example code:

 self.background = [UIImage imageNamed: @"Background.png"];
 [documentDisplay.layer setContents: self.background.CGImage];

I would like to avoid subclassing if I could.

解决方案

You can create a color with a pattern image and set that as the background color of the layer.

To do that you need wrap your CGImage in a CGPattern and make a CGColor from it.

// callback for CreateImagePattern.
static void drawPatternImage (void *info, CGContextRef ctx)
{
    CGImageRef image = (CGImageRef) info;
    CGContextDrawImage(ctx, 
                       CGRectMake(0,0, CGImageGetWidth(image),CGImageGetHeight(image)),
                       image);
}

// callback for CreateImagePattern.
static void releasePatternImage( void *info )
{
    CGImageRelease((CGImageRef)info);
}

//assume image is the CGImage you want to assign as the layer background
int width = CGImageGetWidth(image);
int height = CGImageGetHeight(image);
static const CGPatternCallbacks callbacks = {0, &drawPatternImage, &releasePatternImage};
CGPatternRef pattern = CGPatternCreate (image,
                        CGRectMake (0, 0, width, height),
                        CGAffineTransformMake (1, 0, 0, 1, 0, 0),
                        width,
                        height,
                        kCGPatternTilingConstantSpacing,
                        true,
                        &callbacks);
CGColorSpaceRef space = CGColorSpaceCreatePattern(NULL);
CGFloat components[1] = {1.0};
CGColorRef color = CGColorCreateWithPattern(space, pattern, components);
CGColorSpaceRelease(space);
CGPatternRelease(pattern);
yourLayer.backgroundColor = color; //set your layer's background to the image
CGColorRelease(color);

This code is modified from the GeekGameBoard sample code.

这篇关于如何平铺CALayer的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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