Set the graphics context to display UIImage (Obj-C)

I am trying to do UIImage in Objective-C (working on a simple iPhone application (a thing like a breakthrough) to get used to the environment). However, I get the error "Error: CGContextDrawImage: invalid context" when I try to draw it.

My question is: how to set a context that uses the DrawAtPoint method for UIImage?

Here is the appropriate code for how I initialize / call 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 as:

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

(Ball inherits from GameItem until it does nothing else)

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

Thanks!

+1
objective-c iphone uiimage
source share
2 answers

If you need to do this on the screen, you need to find the drawing code in the -drawRect: method of the UIView (or โ€“drawInContext: for CALayer ). To update its contents, you need to call -setNeedsDisplay on a UIView or CALayer . Attempting to draw at any other time will result in an "invalid context" error.

+3
source share

Try wrapping the call in UIGraphicsBeginImageContext() and UIGraphicsEndImageContext() .

+2
source share

All Articles