简体   繁体   中英

How to tile the contents of a CALayer?

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.

The simplest code to accomplish this is:

CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_tile"]].CGColor;

This is rather simpler. But beware, this is a private API, use at your own risk.

Setup as follows

#import <QuartzCore/QuartzCore.h>

CA_EXTERN NSString * const kCAContentsScalingRepeat;

@interface CALayer (CALayerAdditions)
@property(copy) NSString *contentsScaling;
@end

Then just do this

[myLayer setContentsScaling:kCAContentsScalingRepeat];

你可以使用CGContextDrawTiledImage方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM