2012-02-22 37 views
19

Tôi cần vẽ một hình lục giác và tô màu nó bằng một bản dựng màu với hình ảnh làm mẫu. tôi đã làm:Đường dẫn FillPath và đường nét Stroke

CGContextSaveGState(context); 
CGContextSetLineCap(context, kCGLineCapRound); 
CGContextSetFillColorWithColor(context, [[UIColor colorWithPatternImage:[UIImage imageNamed:@"patternerba.png"]] CGColor]); 
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]); 
CGContextSetLineWidth(context, 3.0); 
// drawing hexagon here... 
CGContextStrokePath(context); 
CGContextFillPath(context); 
[[NSString stringWithFormat:@"Foo"] drawAtPoint:innerRect.origin withFont:[UIFont fontWithName:@"Helvetica" size:16]]; 
CGContextRestoreGState(context); 

Nhưng tùy thuộc từ thứ tự của CGContextStrokePath và CGContextFillPath, tôi nhận được một hình lục giác giáp nhưng không điền hoặc điền nhưng không giáp. Làm thế nào tôi có thể sửa lỗi này?

+0

tăng chiều rộng trong CGContextSetLineWidth .. –

Trả lời

31

Hãy thử

CGContextDrawPath(context, kCGPathFillStroke); 

Thay vì

CGContextStrokePath(context); 
CGContextFillPath(context); 
+0

Hoạt động tuyệt vời. Cảm ơn bạn. – IssamTP

+0

Bạn đã thực hiện một ngày của tôi. Cảm ơn –

0

Hoặc sử dụng

CGContextDrawPath(context, kCGPathFillStroke); 

như sch đề nghị, hoặc vẽ hình lục giác một lần nữa trước khi gọi FillPath. StrokePath và FillPath loại bỏ đường dẫn mà bạn đã thêm vào ngữ cảnh, do đó cuộc gọi tiếp theo sẽ âm thầm thất bại mà không có đường dẫn.

CGPathRef path = /* drawing hexagon here */; 
CGContextAddPath(context, path); 
CGContextStrokePath(context); 
CGContextAddPath(context, path); 
CGContextFillPath(context); 

Lưu ý: hai đoạn mã không tương đương và cho kết quả khác nhau.

Các vấn đề liên quan