2014-04-05 59 views
1

我正在爲iOS的繪圖應用程序工作。在這個應用程序中,我需要繪製質感的線條。對於這個我使用this method。但紋理是如此之小,不在形狀。CG中的紋理線條

我想知道我做錯了什麼,我該如何解決它。

這是我更新的代碼 -

 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很少了解。

+0

在您顯示的代碼中,您在將圖像與筆畫混合之前忘記了描邊路徑。 –

回答

2

但是質地太小而不是形狀。

這是因爲您正在將圖像繪製到路徑的邊界矩形中一次。

你正在做的就像是將圖像打印到一個真正有彈性的織物的小方形上,然後將其拉伸到路徑的大小。

使用尺寸與圖像大小相同的矩形會使其恢復「形狀」,但由於圖像很小,所以它仍然很小。而且,如果你只以這種尺寸畫出一次,它將不會覆蓋大部分路徑的整個區域。

您需要填充矩形與模式基於該圖像。 Create a CGPattern that draws the image,然後set the fill pattern to that pattern並填充路徑的邊界矩形。

+0

仍然無法正常工作。我更新了我的代碼。 –

+1

@HarvantS .:你仍然在做錯誤的事情(將圖像繪製到路徑的邊界矩形中),破壞你之前做過的正確事情。刪除該代碼並使用CGPattern。 –