设置图形上下文以显示 UIImage (Obj-C)
我正在尝试在 Objective-C 中渲染 UIImage(开发一个简单的 iPhone 应用程序(突破类型的东西)以适应环境)。但是,当我尝试绘制它时,出现“错误:CGContextDrawImage:无效上下文”错误。
我的问题是:如何设置 UIImage 的 DrawAtPoint 方法使用的上下文?
这里是我如何初始化/调用所有内容的相关代码:
@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"]];
(Ball继承自GameItem,目前没有做任何不同的事情)
我从drawAtPoint调用中获取了无效的上下文。任何可以解释如何设置上下文的帮助/指针将不胜感激。
谢谢!
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.
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
in update:
[sprite drawAtPoint:pos];
And initializing it like:
newBall = [[Ball alloc] initWithPositionAndSprite:CGPointMake(3.0, 4.0) :[UIImage imageNamed:@"ball.png"]];
(Ball inherits from GameItem, doesn't do anything different just yet)
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要将其绘制到屏幕上,您需要在
UIView
的-drawRect:
方法(或–drawInContext:
)。要更新其内容,您需要在CALayer
的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 aUIView
(or–drawInContext:
of aCALayer
). To update its contents, you'd need to call-setNeedsDisplay
on theUIView
orCALayer
. Attempting drawing at any other time will cause the "invalid context" error you're seeing.尝试将调用包装在
UIGraphicsBeginImageContext()
和UIGraphicsEndImageContext()
中。Try wrapping the call in
UIGraphicsBeginImageContext()
andUIGraphicsEndImageContext()
.