有人可以告诉我如何创建一个 CGPattern 来用图像描画路径吗?

发布于 2024-08-23 06:41:32 字数 51 浏览 6 评论 0原文

我想使用我拥有的 .png 绘制路径,但我只是不知道如何制作 CGPatternRef。

I want to stroke a path using a .png that I have but I just don't know hot to make a CGPatternRef.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛泛之交 2024-08-30 06:41:32

这是一个小片段

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self patternMake2:rect context:context];
}
//-------------------------------------------------------------------
//      patternMake2
//-------------------------------------------------------------------
void pattern2Callback (void *info, CGContextRef context) {      
    UIImage *image = [UIImage imageNamed:@"NavBarBg.png"];
    CGImageRef imageRef = [image CGImage];
    CGContextDrawImage(context, CGRectMake(0, 0, 320, 44), imageRef);
}


- (void)patternMake2:(CGRect)rect context:(CGContextRef)context
{
    static const CGPatternCallbacks callbacks = { 0, &pattern2Callback, NULL };
    //NSLog(@"rect: %f %f %f %f", rect.origin.x, rect.origin.x, rect.size.width, rect.size.height); 
    //CGContextSaveGState(context);
    CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
    CGContextSetFillColorSpace(context, patternSpace);
    CGColorSpaceRelease(patternSpace);
    CGSize patternSize = CGSizeMake(315, 44);
    CGPatternRef pattern = CGPatternCreate(NULL, self.bounds, CGAffineTransformIdentity, patternSize.width, patternSize.height, kCGPatternTilingConstantSpacing, true, &callbacks);
    CGFloat alpha = 1;
    CGContextSetFillPattern(context, pattern, &alpha);
    CGPatternRelease(pattern);
    CGContextFillRect(context, rect);       
    //CGContextRestoreGState(context);
}

here's a little snippet

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self patternMake2:rect context:context];
}
//-------------------------------------------------------------------
//      patternMake2
//-------------------------------------------------------------------
void pattern2Callback (void *info, CGContextRef context) {      
    UIImage *image = [UIImage imageNamed:@"NavBarBg.png"];
    CGImageRef imageRef = [image CGImage];
    CGContextDrawImage(context, CGRectMake(0, 0, 320, 44), imageRef);
}


- (void)patternMake2:(CGRect)rect context:(CGContextRef)context
{
    static const CGPatternCallbacks callbacks = { 0, &pattern2Callback, NULL };
    //NSLog(@"rect: %f %f %f %f", rect.origin.x, rect.origin.x, rect.size.width, rect.size.height); 
    //CGContextSaveGState(context);
    CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
    CGContextSetFillColorSpace(context, patternSpace);
    CGColorSpaceRelease(patternSpace);
    CGSize patternSize = CGSizeMake(315, 44);
    CGPatternRef pattern = CGPatternCreate(NULL, self.bounds, CGAffineTransformIdentity, patternSize.width, patternSize.height, kCGPatternTilingConstantSpacing, true, &callbacks);
    CGFloat alpha = 1;
    CGContextSetFillPattern(context, pattern, &alpha);
    CGPatternRelease(pattern);
    CGContextFillRect(context, rect);       
    //CGContextRestoreGState(context);
}
酒儿 2024-08-30 06:41:32

请参阅Quartz 2D 编程指南的相关章节CGPattern 的参考文档

编程指南中“LOOK PRETTY PATTERNS”一页又一页的基本细节是,您需要编写一个回调函数来绘制该模式的一个实例,并将指向该回调的指针传递给CGPatternCreate.当您绘制图案时,Quartz 将调用您的回调,然后平铺您绘制的任何内容。

See the relevant chapter of the Quartz 2D Programming Guide and the reference documentation for CGPattern.

The essential detail that's buried in the Programming Guide under pages and pages of “LOOK PRETTY PATTERNS” is that you need to write a callback function that draws one instance of the pattern, and pass the pointer to that callback to CGPatternCreate. When you draw the pattern, Quartz will call your callback, then tile whatever you drew.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文