gpt4 book ai didi

iphone - 如何在 CATiledLayer 中绘制平铺图案图像?

转载 作者:行者123 更新时间:2023-12-03 20:26:06 25 4
gpt4 key购买 nike

我正在按照 this answer 中概述的说明进行操作用 Core Graphics 中的图案填充图层。当图层是 CALayer 子类时,绘图工作正常。但是,当图层是 CATiledLayer 子类时,我在运行时收到 EXC_BAD_ACCESS 错误。

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);

我需要做什么才能在 CATiledLayer 子类中绘制图案图像?

最佳答案

我在 CATiledLayer 类的“drawLayer”方法中使用“CGContextDrawTiledImage”:

在.h中:

@interface StarView : UIView {
UIImage *imgTextureCarta;
}

以 .m 为单位

- (id)initWithFrame:(CGRect)frame {
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:@"aTexture" ofType:@"png"];
imgTextureCarta = [UIImage imageWithContentsOfFile: imagePath];
[imgTextureCarta retain];
CGImageRef texureCarta;
texureCarta = (imgTextureCarta.CGImage);
}



-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context{
CGImageRef texureCarta;
texureCarta = (imgTextureCarta.CGImage);
CGRect bounds = layer.bounds;
CGContextSetBlendMode (context, 0);
CGContextClipToRect(context, layer.bounds);
CGContextDrawTiledImage(context, CGRectMake(0,0, 128, 120), texureCarta);
}

- (void)dealloc {
[imgTextureCarta release];
[super dealloc];
}

关于iphone - 如何在 CATiledLayer 中绘制平铺图案图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5288290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com