I’m following the instructions outlined in this answer to fill a layer with a pattern in Core Graphics. When the layer is a CALayer subclass, the drawing works fine. However, when the layer is a CATiledLayer subclass, I get an EXC_BAD_ACCESS error at runtime.
static void drawPatternImage (void *info, CGContextRef ctx)
{
CGImageRef image = (CGImageRef) info;
CGContextDrawImage(ctx,
CGRectMake(0,0, CGImageGetWidth(image),CGImageGetHeight(image)),
image); // EXC_BAD_ACCESS here :(
}
static void releasePatternImage( void *info )
{
CGImageRelease((CGImageRef)info);
}
// pattern creation
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);
theLayer.backgroundColor = color;
CGColorRelease(color);
What do I need to do to draw a patterned image in a CATiledLayer subclass?
i use “CGContextDrawTiledImage” in the “drawLayer” method for a CATiledLayer class:
in .h:
in .m