我如何在DrawRect之外做DrawRect类型的东西?
我有一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
继续在
-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.确保图像上下文的尺寸不是无效的。例如,如果它们是 {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.
也许您正在寻找 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.
把它放在 initWithFrame: 中怎么样?这对你有用吗?
How about putting it in initWithFrame:? Would that work for you?