CALayer全体にCGImageを並べて表示したいのですが、拡大したいだけのようです。
サンプルコード:
self.background = [UIImage imageNamed: @"Background.png"];
[documentDisplay.layer setContents: self.background.CGImage];
できればサブクラス化は避けたいと思います。
パターン画像で色を作成し、それをレイヤーの背景色として設定できます。
CGImage
それを行うには、をラップして、それからCGPattern
作成する必要がありますCGColor
。
// 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);
このコードは、GeekGameBoardサンプルコードから変更されています。
これを実現するための最も簡単なコードは次のとおりです。
CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_tile"]].CGColor;
これはかなり簡単です。ただし、これはプライベートAPIであり、自己責任で使用してください。
次のように設定します
#import <QuartzCore/QuartzCore.h>
CA_EXTERN NSString * const kCAContentsScalingRepeat;
@interface CALayer (CALayerAdditions)
@property(copy) NSString *contentsScaling;
@end
次に、これを実行します
[myLayer setContentsScaling:kCAContentsScalingRepeat];
これにはCGContextDrawTiledImageメソッドを使用できます