【问题标题】:Cannot draw an image.无法绘制图像。
【发布时间】:2014-04-02 16:29:21
【问题描述】:

我在UIViewController 中有一个UIImageView,并且想在现有的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 模拟器(其他线程中关于此的解决方案),但这并没有解决问题。

【问题讨论】:

  • 您想在UIImageView 上显示UIImage 还是在它当前显示的内容之上呈现它?
  • 您应该能够通过将代码包装在对 UIGraphicsBeginImageContext() 和 UIGraphicsEndImageContext() 的调用中来解决此问题。
  • @Alladinian:第二种选择。在它当前显示的内容之上。
  • geraldWilliam,你有例子吗?
  • 您是否可以选择使用第二个 UIImageView?

标签: ios iphone uiviewcontroller uiimageview cgcontextdrawimage


【解决方案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];

【讨论】:

  • 谢谢,这确实显示了图像。但是它应该在顶部的图像现在是黑色的。即使我将它作为子视图添加到 ImageView(现在是黑色的图像视图),它也是黑色的。这是为什么呢?
  • 哦,对了。本质上,两个图像都需要绘制到同一个视图中。有关如何实现“带水印”图像的示例,请参阅此论坛答案 [@DuncanC 的iphonedevsdk.com/forum/iphone-sdk-development/…。
猜你喜欢
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 2017-01-31
  • 2023-03-16
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多