Justin
Justin

Reputation: 2142

How to "draw" UIImages into a view?

I am making a simple game, but I need it to draw images twenty times every second. I was going to use multiple UIImageViews, but I quickly realized that it would not work. So, how can a draw images, then "erase" them when the update timer fires? I tried to use the following code, but nothing showed up.

- (void) update:(NSTimer*) timer {
    UIimage *someImage;
    someImage = [UIImage imageNamed:@"car.png"];
    [someImage drawAtPoint:thePoint];
}

I have tried to make thePoint both an NSPoint and a CGRect, but nothing showed up. What do I do now? Note: thePoint is declared in my .h file. It is currently an NSPoint, and I think it is being used properly.

Upvotes: 0

Views: 763

Answers (2)

sudo rm -rf
sudo rm -rf

Reputation: 29524

NSPoint is for Mac, and CGRect is, well, a rectangle and not a point. What you're looking for is a CGPoint. Try something like this:

-(void)update:(NSTimer*) timer {
    CGPoint thePoint = CGPointMake(0, 0);
    UIImage *someImage = [UIImage imageNamed:@"car.png"];
    [someImage drawAtPoint:thePoint];
}

Upvotes: 1

jscs
jscs

Reputation: 64002

Is this really your code? It makes almost no sense. You've made a local variable, someImage, haven't created an object for it to point to, and are then trying to call a method on this non-object that doesn't exist on the class you're trying to use (UIImage has no setImage:). It's almost guaranteed to crash, and if it doesn't right away, then it very well might cause one later because it's clobbering memory -- a variable created on the stack like this doesn't get initialized to 0 (nil) as it would if it were an ivar, and trying to send a message to some random address is a bad idea. Why would you try passing a CGRect to a method that even says in its name that it takes a point? Finally, where do you expect this to be drawn? You need a drawing context (usually provided by a view, which is mentioned in your title, but not the question) for drawing to go into.

Please have a look at the "Images" chapter of the Drawing Guide, if nothing else.

Upvotes: 2

Related Questions