无法绘制图像。

时间:2014-04-02 16:29:21

标签: ios iphone uiviewcontroller uiimageview cgcontextdrawimage

我在UIImageView中有一个UIViewController,想要在现有的imageView上添加一个简单的图片。

viewDidLoad方法如下所示:

    UIImage *test;
    test = [UIImage imageNamed:@"position-dot.png"];

    [test drawAtPoint:CGPointMake(100,100) ];

我收到的错误消息的一部分如下所示:

CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

CGContextSetBlendMode: invalid context 0x0. This is a serious error

我测试了重置我的iOS模拟器(关于此的其他线程中的解决方案),但这并没有解决问题。

1 个答案:

答案 0 :(得分:1)

虽然上面评论中的方法确实使警告静音,但我发现图像没有像我预期的那样显示。我发现使drawAtPoint工作的最简单方法是将UIView子类化,并将确切的代码放入drawRect的实现中:就像这样。

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    UIImage *test;
    test = [UIImage imageNamed:@"position-dot"];
    [test drawAtPoint:CGPointMake(100.0, 100.0)];
}

然后,在你的控制器中:

MyViewSubclass *view = [[MyViewSubclass alloc] initWithFrame:self.view.frame];
[self.view addSubview:view];