iphone:使用Core Graphics重写GLPaint
我想重写Apple源代码,GLPaint应用程序,但不使用像OpenGL这样的例子,我想使用Core Graphics库。 但我坚持像GLPaint那样展示纹理。
这是我在Core Graphics中的代码:
- (void) drawLineFromPoint: (CGPoint) fromPoint toPoint: (CGPoint) toPoint { UIGraphicsBeginImageContext(self.frame.size); CGContextRef context = UIGraphicsGetCurrentContext(); [drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; UIImage * image = [UIImage imageNamed:@"Particle.png"]; UIColor * color = [UIColor colorWithPatternImage:image]; CGContextSetStrokeColorWithColor(context, color.CGColor); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineWidth(context, 5.0); CGContextBeginPath(context); CGContextMoveToPoint(context, fromPoint.x, fromPoint.y); CGContextAddLineToPoint(context, toPoint.x, toPoint.y); CGContextStrokePath(context); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); }
—但是我得到了这个结果:
—我希望得到这样的结果:
我的代码错了吗? 任何人都可以帮助我吗? 非常感谢你。
如果您不想要图案,请不要将图案图像的颜色设置为笔触颜色。
代替
UIImage * image = [UIImage imageNamed:@"Particle.png"]; UIColor * color = [UIColor colorWithPatternImage:image];
做就是了
UIColor *color = [UIColor blackColor];
请确保将Particle.png添加到项目中。 还要检查GLPaint代码中的常量kBrushScale和kPalette * 。