我如何在DrawRect之外做Draw​​Rect类型的东西?

发布于 2024-11-08 14:01:30 字数 911 浏览 0 评论 0原文

我有一个基于 UIView 的类,称为 Icon。在那里,我想要有与此类似的代码:

UIView *finalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
finalView.backgroundColor = [UIColor redColor];

UIGraphicsBeginImageContext(finalView.bounds.size);
[finalView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

问题是,我不想将它放在 drawRect 中,因为当我的应用程序在另一个视图中制作每个图标时会调用它。我如何将此类代码放在我的 Icon 类中的其他位置?我试着把它放在 1 个地方,它给了我:

CGContextSaveGState: invalid context 0x0
CGContextSetAlpha: invalid context 0x0
CGContextSaveGState: invalid context 0x0
CGContextSetFillColorWithColor: invalid context 0x0
CGContextAddRect: invalid context 0x0
CGContextDrawPath: invalid context 0x0
CGContextRestoreGState: invalid context 0x0
CGContextRestoreGState: invalid context 0x0

I have a UIView-based class called Icon. In there, I want to have code similar to this:

UIView *finalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
finalView.backgroundColor = [UIColor redColor];

UIGraphicsBeginImageContext(finalView.bounds.size);
[finalView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The problem is, i don't want to have to put it in drawRect because that's called when my app makes each Icon in another view. How can i put this sort of code somewhere else in my Icon class? I tried putting it in 1 place and it gave me:

CGContextSaveGState: invalid context 0x0
CGContextSetAlpha: invalid context 0x0
CGContextSaveGState: invalid context 0x0
CGContextSetFillColorWithColor: invalid context 0x0
CGContextAddRect: invalid context 0x0
CGContextDrawPath: invalid context 0x0
CGContextRestoreGState: invalid context 0x0
CGContextRestoreGState: invalid context 0x0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℉絮湮 2024-11-15 14:01:30

继续在 -drawRect: 中完成所有绘图。当某些内容发生变化并且您希望重新绘制视图时,请向其发送 -setNeedsDisplay-setNeedsDisplayInRect: 消息。

Keep doing all your drawing in -drawRect:. When something changes and you want your view to be re-drawn, sent it a -setNeedsDisplay or -setNeedsDisplayInRect: message.

橘寄 2024-11-15 14:01:30

确保图像上下文的尺寸不是无效的。例如,如果它们是 {0,0},那么您将在创建上下文时得到 NULL 响应,并且所有需要有效位图上下文的调用都会告诉您 0x0 不是有效上下文。

Make sure that the dimensions of the image context are not invalid. If they are for example {0,0} then you will get a NULL response creating the context and all calls needing a valid bitmap context will tell you that 0x0 is not a valid context.

顾冷 2024-11-15 14:01:30

也许您正在寻找 UIGraphicsPushContext(context)?您可以将图像上下文设为当前图像上下文,以便在 UIKit 可用的任何地方进行绘制。绘图后不要忘记 UIGraphicsPopContext()。

Maybe you are looking for UIGraphicsPushContext(context)? You can make your image context current to draw on it at any place where UIKit is available. Do not forget to UIGraphicsPopContext() after your drawing.

套路撩心 2024-11-15 14:01:30

把它放在 initWithFrame: 中怎么样?这对你有用吗?

How about putting it in initWithFrame:? Would that work for you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文