Puede crear un color con una imagen de patrón y establecerlo como el color de fondo de la capa.
Para hacer eso necesita envolver su CGImage
en un CGPattern
y hacer un CGColor
desde él.
// 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);
Este código se ha modificado a partir del código de muestra GeekGameBoard.
acabo de notar + colorWithPatternImage en UIColor, es esta cosa ¿similar? –
Sí, esto debería hacer lo mismo, aunque no lo he probado. 'UIColor' no siempre ha estado disponible en iPhone OS, así que estoy acostumbrado a usar los métodos' CGColor'. –
No estoy del todo seguro, pero me parece que el uso de imágenes como 'backgroundColor' rompe la capacidad de animarlo. (He publicado una pregunta sobre eso [aquí] (http://stackoverflow.com/questions/5605618/core-animation-animation-doesnt-work).) – ryyst