【问题标题】:Colors for drawing app绘图应用程序的颜色
【发布时间】:2023-03-11 23:35:02
【问题描述】:

对于我的应用,我需要实现一个绘图空间。在在线教程的帮助下,我成功绘图。但现在的挑战是用不同的颜色来绘制。我在下面编写了代码,但它不起作用,我不明白为什么它不起作用。它只涂成黑色。 我的 UIView.m 中有以下内容

    - (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 是如何被调用的。
  • 你的问题一清二楚。代码[color setStroke] 中的“颜色”是什么?为什么你们都将增量图像绘制到上下文中,然后将其作为绘图的输出获取?

标签: ios objective-c xcode colors draw


【解决方案1】:

我怀疑虽然您创建了一个位图上下文,但您实际上并没有绘制它.. 试试这个

    - (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();
    }

【讨论】:

  • 用不兼容类型'void'的表达式初始化'CGContextRef'(又名'struct CGContext *'),我做错了吗?
猜你喜欢
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 2020-08-05
  • 1970-01-01
  • 2012-04-02
相关资源
最近更新 更多