设置图形上下文以显示UIImage(Obj-C) [英] Set graphics context to display UIImage (Obj-C)

查看:74
本文介绍了设置图形上下文以显示UIImage(Obj-C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Objective-C中渲染UIImage(在一个简单的iPhone应用程序(突破型的东西)上工作以适应环境).但是,尝试绘制时出现错误:CGContextDrawImage:无效上下文"错误.

I'm trying to render a UIImage in Objective-C (working on a simple iPhone app (breakout-type thing) to get used to the environment). However, I'm getting an "Error: CGContextDrawImage: invalid context" error when I try and draw it.

我的问题是:如何设置UIImage的DrawAtPoint方法使用的上下文?

My question is: how do I set the context that the DrawAtPoint method of UIImage uses?

以下是有关我如何初始化/调用所有内容的相关代码:

Here the relevant code for how I'm initializing / calling everything:

@interface GameItem : NSObject
{
    IBOutlet UIImage    * sprite;
    CGPoint                 pos;
}

- (id) initWithPositionAndSprite: (CGPoint)placementPosition :(UIImage*) blockImage;
- (void) update;

@property CGPoint pos;

@end

更新中:

[sprite drawAtPoint:pos];

并将其初始化为:

newBall = [[Ball alloc] initWithPositionAndSprite:CGPointMake(3.0, 4.0) :[UIImage imageNamed:@"ball.png"]];

(球从GameItem继承,暂时没有做任何不同的事情)

(Ball inherits from GameItem, doesn't do anything different just yet)

我从drawAtPoint调用afaik获取了无效的上下文.任何能够解释如何设置上下文的帮助/指针将不胜感激.

I'm getting the invalid context from the drawAtPoint call afaik. Any help/pointers to somewhere that will explain how to set the context would be greatly appreciated.

谢谢!

推荐答案

如果要在屏幕上绘制此图形,则需要在 -drawRect:方法的内部找到您的绘制代码. UIView (或 CALayer –drawInContext:).要更新其内容,您需要在 UIView CALayer 上调用 -setNeedsDisplay .在任何其他时间尝试绘图都会导致您看到无效的上下文"错误.

If this is to be drawn to the screen, you'll need to locate your drawing code within the -drawRect: method of a UIView (or –drawInContext: of a CALayer). To update its contents, you'd need to call -setNeedsDisplay on the UIView or CALayer. Attempting drawing at any other time will cause the "invalid context" error you're seeing.

这篇关于设置图形上下文以显示UIImage(Obj-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆