10

CALayer全体にCGImageを並べて表示したいのですが、拡大したいだけのようです。

サンプルコード:

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

できればサブクラス化は避けたいと思います。

4

4 に答える 4

13

パターン画像で色を作成し、それをレイヤーの背景色として設定できます。

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サンプルコードから変更されています。

于 2010-03-26T03:52:54.890 に答える
10

これを実現するための最も簡単なコードは次のとおりです。

CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_tile"]].CGColor;
于 2013-02-05T19:23:13.740 に答える
3

これはかなり簡単です。ただし、これはプライベートAPIであり、自己責任で使用してください。

次のように設定します

#import <QuartzCore/QuartzCore.h>

CA_EXTERN NSString * const kCAContentsScalingRepeat;

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

次に、これを実行します

[myLayer setContentsScaling:kCAContentsScalingRepeat];
于 2012-07-01T06:37:00.190 に答える
0

これにはCGContextDrawTiledImageメソッドを使用できます

于 2011-05-21T12:14:24.427 に答える