绘制UIImage时无效的上下文错误 [英] invalid context error when drawing a UIImage
问题描述
我试图画一个UIImage到我的UIView的上下文。我使用这个代码与上下文的东西注释和输出...
- (void)drawRect:(CGRect) rect
{
//UIGraphicsBeginImageContextWithOptions(rect.size,YES,[[UIScreen mainScreen] scale]);
//UIGraphicsBeginImageContext(rect.size);
UIImage * newImage = [UIImage imageNamed:@character_1_0001.png];
// [newImage drawAtPoint:CGPointMake(200,200)];
[newImage drawInRect:rect];
// UIGraphicsEndImageContext();
}
根据我的理解,我不需要设置图像上下文这个,确实没有它我看到在视图中绘制的图像,但我也得到这个错误在日志...
<错误> ;: CGContextSaveGState:无效上下文0x0
如果我取消注释UIGraphicsBeginImageContext行,任何绘制的和没有错误。
我需要使用默认的图形上下文,并以这种方式声明?我需要绘制图像,因为我想在上下文中添加它,不能只生成一个UIImageView对象的负载。
听起来你直接调用 drawRect:
。所以它从你的电话,一次从真正的绘图循环被调用一次。
- 随着上下文创建的存在,你可以在一个新的上下文中绘制所有的图形,然后丢弃它,
- 在上下文创建缺失的情况下,您会从手动调用
drawRect:
得到错误,实际绘图发生在真正的<$ c $ c> drawRect:,以便您看到自己的图片。
< drawRect:。在适当的时候调用
setNeedsDisplay
和 drawRect:
。 I am trying to draw a UIImage to the context of my UIView. I've used this code with the context stuff commented in and out ...
- (void)drawRect:(CGRect)rect
{
//UIGraphicsBeginImageContextWithOptions(rect.size,YES,[[UIScreen mainScreen] scale]);
//UIGraphicsBeginImageContext(rect.size);
UIImage *newImage = [UIImage imageNamed:@"character_1_0001.png"];
//[newImage drawAtPoint:CGPointMake(200, 200)];
[newImage drawInRect:rect];
//UIGraphicsEndImageContext();
}
As I understand it I shouldn't need to set the image context to do this and indeed without it I do see the image drawn in the view but I also get this error in the log ...
<Error>: CGContextSaveGState: invalid context 0x0
If I uncomment the UIGraphicsBeginImageContext lines I don't get anything drawn and no error.
Do I need to use the default graphics context and declare this somehow?
I need to draw the image because I want to add to it on the context and can't just generate a load of UIImageView objects.
It sounds like you are calling drawRect:
directly. So it is getting called once from your call, and once from the genuine drawing loop.
- With the context creation present, you do all the drawing in a new context then discard it, so you never see anything
- With the context creation missing, you get the error from the manually called
drawRect:
, and the actual drawing takes place in the genuinedrawRect:
so you see your image.
Don't call drawRect:
directly. Call setNeedsDisplay
and drawRect:
will be called for you at the appropriate point.
这篇关于绘制UIImage时无效的上下文错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!