ios - 无法绘制图像。

标签 ios iphone uiviewcontroller uiimageview cgcontextdrawimage

我在 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:无效上下文 0x0。这是一个严重的错误

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

最佳答案

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

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

然后,在您的 Controller 中:

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

关于ios - 无法绘制图像。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22817574/

相关文章:

ios - 如何将以编程方式创建的 View Controller 的 View 设置为 SKYView?

java - Gmail API 服务器访问 "invalid_grant"iOS?

javascript - 如何在 Phonegap 应用程序中查找内存泄漏

iphone - UISearchBar 范围按钮不会显示 iOS 6

iphone - Cordova/Phonegap iPhone 闪屏错误

ios - 由于在应用程序的其他位置使用了UIMenuController,因此UISearchBar显示错误的UIMenuItems

ios - 从代码实例化 View Controller - viewDidLoad 何时调用 iOS

ios - 如何为选定的 tableView 行创建自定义图像复选标记附件?

iOS 模拟器 - 无法为应用程序分配内存

ios - 收集几个 Alamofire 请求的所有响应