gpt4 book ai didi

iphone - 绘制矩形时出现问题

转载 作者:行者123 更新时间:2023-12-03 19:32:09 25 4
gpt4 key购买 nike

我在 viewLoad 中使用下面的代码(我不想在 - (void)drawRect:(CGRect)rect 中绘制)来在 View 上添加矩形,但不起作用。为什么?

CGRect rect=CGRectMake(10,10,150,140);
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(context, red);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 10, 10);
CGContextAddRect(context, rect);
CGContextStrokePath(context);

最佳答案

只有在拥有绘图上下文时才能进行绘图。您可以在任何想要创建图像的地方创建图像上下文,也可以在 drawRect: 内绘制以绘制到您的 View 。您不应该在 drawRect: 之外绘制 View 。

看起来您似乎获得了上下文,但实际上您没有获得 UIGraphicsGetCurrentContext() returns nil outside of drawRect: (或者更正确地说:它只返回 drawRect: 内的上下文,或者如果您自己创建并推送了图像上下文)。

关于iphone - 绘制矩形时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5687642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com