iphone:将纹理应用于CGContextStrokePath [英] iphone: Apply a texture to CGContextStrokePath
问题描述
我在StackOverflow中搜索所有问题和答案,但无法得到我想要的结果。我想使用PNG文件,在用户触摸和移动点显示为笔划。这是PNG文件:
I search all the question and answer in StackOverflow but can not get the result I want. I would like to use a PNG file, to display as a stroke at the point user touch and move. This is the PNG file:
但我在StackOverflow上做了所有的方法,即使使用CGPatternRef,但我没有我想要的结果。我得到的结果是:
But I did all the ways on StackOverflow, even using CGPatternRef, but I don't have the result I want. The result I have is here:
但我想要的结果是:
这个结果我在OpenGL ES中完成了,但我不能在Core Graphics中做到这一点。我想我可以在每个点使用CGContextDrawImage,但这种方式导致性能不佳,因为我必须在用户触摸的2点之间绘制所有点的图像。我想你以前遇到过这个问题。你认为我可以在Core Graphics中应用纹理来绘制这样的笔画吗?非常感谢。
This result I have done in OpenGL ES, but I can not do that in Core Graphics. I think I can use CGContextDrawImage at each point, but this way cause poor performance because I must draw image all the points between 2 points user touched. I think you have encountered this problem before. Do you think I can apply the texture to draw stroke like this in Core Graphics? I am very appreciated.
推荐答案
使用模式创建线条时,它看起来像第一个图像,而不是第二。该线条简单地是您提供的纹理图像的平铺,将其裁剪成线条的形状。没有办法知道你想要将圆圈合并在一起看起来像一条实线。为此你应该只用一个纯黑色的线。
When you create a line using a pattern it is going to look like the first image, not the second. The line is simply a tiling of the texture image you provide cropped out into the shape of a line. There is no way for it to know that you meant to merge the circles together to look like a solid line. For that you should probably just make a line with a solid black color.
对于任何试图在第一张图片中实现图案线条的人来说,有一个很好的教程解释如何在此处执行此操作: http://blog.robertturrall.com/tag/cgcontextsetstrokepattern/
For anyone trying to achieve the patterned line in the first image there is an excellent tutorial explaining how to do this here: http://blog.robertturrall.com/tag/cgcontextsetstrokepattern/
基本上你需要的是:
// first define the pattern width and height
const float kPatternWidth = 8;
const float kPatternHeight = 8;
void DrawPatternCellCallback(void *info, CGContextRef cgContext)
{
// Create a CGImage and use CGContextDrawImage() to draw it into the graphics context provided by the callback function.
UIImage *patternImage = [UIImage imageNamed:@"yourtextureimage.png"];
CGContextDrawImage(cgContext, CGRectMake(0, 0, kPatternWidth, kPatternHeight), patternImage.CGImage);
}
确保void周围没有括号。还要确保DrawPatternCellCallback部分在drawRect之前出现,否则它将无效。
Make sure "void" has no parentheses around it. Also make sure the DrawPatternCellCallback part comes before drawRect or it won't work.
然后你的drawRect需要以下内容:
Then your drawRect needs the following:
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
const CGRect patternBounds = CGRectMake(0, 0, kPatternWidth, kPatternHeight);
const CGPatternCallbacks kPatternCallbacks = {0, DrawPatternCellCallback, NULL};
CGAffineTransform patternTransform = CGAffineTransformIdentity;
CGPatternRef strokePattern = CGPatternCreate(
NULL,
patternBounds,
patternTransform,
kPatternWidth, // horizontal spacing
kPatternHeight, // vertical spacing
kCGPatternTilingNoDistortion,
true,
&kPatternCallbacks);
CGFloat color1[] = {1.0, 1.0, 1.0, 1.0};
CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
CGContextSetStrokeColorSpace(ctx, patternSpace);
CGContextSetStrokePattern(ctx, strokePattern, color1);
CGContextSetLineWidth(ctx, 4.0);
CGContextDrawPath(ctx);
// If you aren't using ARC:
CGPatternRelease(strokePattern);
strokePattern = NULL;
CGColorSpaceRelease(patternSpace);
patternSpace = NULL;
}
这篇关于iphone:将纹理应用于CGContextStrokePath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!