Ios 绘图应用程序的颜色
对于我的应用程序,我需要实现一个绘图空间。在一个在线教程的帮助下,我成功地完成了绘画。但现在的挑战是用不同的颜色画画。我写了下面的代码,但它不工作,我不明白为什么它不工作。它只画黑色。 我的UIView中有以下内容Ios 绘图应用程序的颜色,ios,objective-c,xcode,colors,draw,Ios,Objective C,Xcode,Colors,Draw,对于我的应用程序,我需要实现一个绘图空间。在一个在线教程的帮助下,我成功地完成了绘画。但现在的挑战是用不同的颜色画画。我写了下面的代码,但它不工作,我不明白为什么它不工作。它只画黑色。 我的UIView中有以下内容 - (void)drawBitmap { UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0); if (!incrementalImage) // first time; pa
- (void)drawBitmap
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
if (!incrementalImage) // first time; paint background white
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];
[[UIColor whiteColor] setFill];
[rectpath fill];
}
[incrementalImage drawAtPoint:CGPointZero];
[color setStroke];
[path stroke];
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
- (void)color1 {
color = [UIColor greenColor];
}
- (void)color2 {
color = [UIColor yellowColor];
}
我从ViewController.m调用函数
- (IBAction)drawGreen:(id)sender {
SmoothedBIView *smoothView = [[SmoothedBIView alloc]init];
[smoothView color1];
}
- (IBAction)drawYellow:(id)sender {
SmoothedBIView *smoothView = [[SmoothedBIView alloc] init];
[smoothView color2];
}
谢谢你的帮助 我怀疑,尽管创建了位图上下文,但实际上并没有绘制到它。。 试试这个
“它不起作用”是什么意思?预期的结果是什么,实际的结果是什么?颜色只是黑色,不会变为另一种颜色。您还没有演示如何调用函数color1和color2。您的问题非常清楚。代码中的“颜色”是什么?为什么你们都在上下文中绘制增量图像,然后将其作为绘图的输出?用不兼容类型“void”的表达式初始化“CGContextRef”(又名“struct CGContext*),我做错了什么吗?
- (void)drawBitmap
{
CGContextRef ctx = UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);//changed
UIGraphicsPushContext(ctx);//changed
if (!incrementalImage) // first time; paint background white
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];
[[UIColor whiteColor] setFill];
[rectpath fill];
}
[incrementalImage drawAtPoint:CGPointZero];
[color setStroke];
[path stroke];
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPopContext(ctx); //changed
UIGraphicsEndImageContext();
}