Sie können eine Farbe mit einem Musterbild erstellen und diese als Hintergrundfarbe für die Ebene festlegen.
Dazu müssen Sie Ihre CGImage
in eine CGPattern
wickeln und daraus eine CGColor
machen.
// 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);
Dieser Code wurde vom GeekGameBoard-Beispielcode geändert.
Ich habe gerade bemerkt + colorWithPatternImage in UIColor, ist dieses Ding ähnlich? –
Ja, das sollte das gleiche tun, obwohl ich es nicht getestet habe. "UIColor" war in iPhone OS nicht immer verfügbar, daher bin ich es gewohnt, die CGColor-Methoden zu verwenden. –
Ich bin mir nicht ganz sicher, aber es scheint mir, dass die Verwendung von Bildern als 'backgroundColor' die Fähigkeit, es zu animieren, bricht. (Ich habe eine Frage dazu gestellt [http://stackoverflow.com/questions/5605618/core-animation-animation-doesnt-work].) – ryyst