ios - CG 中的纹理线

标签 ios objective-c core-graphics textures drawable

我正在开发适用于 iOS 的绘图应用程序。在这个应用程序中,我需要绘制纹理线条。为此,我正在使用 this method .But texture 太小了而且形状不对。

我想知道我做错了什么,我该如何解决。

这是我更新后的代码-

        CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
        CGPoint mid2 = midPoint(currentPoint, previousPoint1);


        [curImage drawAtPoint:CGPointMake(0, 0)];
        CGContextRef context = UIGraphicsGetCurrentContext();




        [self.layer renderInContext:context];
        CGContextMoveToPoint(context, mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 

        CGContextSetLineCap(context, kCGLineCapRound);


        CGContextSetLineJoin(context, kCGLineJoinRound);
        CGContextSetLineWidth(context, self.lineWidth);
        //trans layer
        CGContextBeginTransparencyLayer(context, nil);//
        CGRect rect=CGContextGetPathBoundingBox (context);//
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//
        //trans layer end

        CGContextSetShouldAntialias(context, YES);  
        CGContextSetAllowsAntialiasing(context, YES);
        CGContextSetFlatness(context, 0.1f);
        CGContextSetAlpha(context, self.lineAlpha);
        CGContextStrokePath(context);


       //patter start
        CGContextSetFillColorSpace(context, CGColorSpaceCreatePattern(NULL));
        static const CGPatternCallbacks callbacks = { 0, &lightDataArea, NULL };

        CGPatternRef hello = CGPatternCreate(NULL, rect, CGAffineTransformIdentity, 0, 0, kCGPatternTilingConstantSpacing, YES, &callbacks);
        CGFloat alpha = 1;


        CGContextSetFillPattern(context, hello, &alpha);
        CGContextFillRect(context, rect);
        //pattern end


        //trans layer remaining
        CGContextSetBlendMode(context, kCGBlendModeSourceIn);//
        CGContextDrawImage(context, rect, [self image:[UIImage imageNamed:@"dddd.jpg"] withColor:[UIColor blueColor]].CGImage);//
        CGContextEndTransparencyLayer (context);//
        //trans layer end

CGPatterCallback -

void lightDataArea (void *info, CGContextRef context)
{
      UIImage *image = [UIImage imageNamed:@"dddd.png"];
      CGImageRef imageRef = [image CGImage];
      CGContextDrawImage(context, CGRectMake(0, 0, 100, 100), imageRef);
}

这是我得到的

enter image description here

我的纹理图像

enter image description here

还是一样的结果。请帮帮我。我对CG知之甚少。

最佳答案

But texture is so small and not in shape.

那是因为您只将图像绘制到路径的边界矩形中一次。

您所做的就像将图像打印到一 block 非常有弹性的小正方形织物上,然后将其拉伸(stretch)到路径的大小。

使用与图像大小相同的矩形会使它恢复“原状”,但它仍然很小,因为图像很小。此外,如果您只以该尺寸绘制一次,它不会覆盖大多数路径的整个区域。

您需要使用基于该图像的图案填充矩形。 Create a CGPattern that draws the image , 然后 set the fill pattern to that pattern并填充路径的边界矩形。

关于ios - CG 中的纹理线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22880587/

相关文章:

ios - CFBundleDocumentTypes 不适用于 iOS 应用程序中的非公共(public) UTI

objective-c - 对类和对象 iVar 感到困惑

iphone - CGContextFillRect 不绘制矩形?

ios - 在 iOS Objective C 中处理位置权限

ios - 如何为 rootViewController 调用 UINavigationControllerDelegate 委托(delegate)方法

ios - 仅包含 @ist 的 NSString 不符合键值

ios - 在 iOS 中绘制沿路径均匀分布的图像

ios - 将颜色加深限制在 CGPath 的内部

ios - 了解代码是否是针对 iPhone、iPad 或 Universal 编译的

objective-c - 另一个 "Retain, then Release"问题