Drawing image with drawAtPoint (cocoa touch)
I'm new to objective-C programming for iOS. I'm struggling with a really simple task, drawing an image with code (not just including it in 'interface builder'). Here's the part of my code where I'm trying to p开发者_StackOverflow社区ut my image into the view:
UIImage *image = [UIImage imageNamed:@"Note.png"];
[image drawAtPoint:CGPointZero];
Simple. I have also tried with some retain and release commands, and even tried to include a second view on top of the old, to draw the image in. Without luck.
Thanks, John.
Where are you doing this? drawAtPoint
will require there to be a valid current drawing context. Most commonly you should call it from inside some view's drawRect
method.
If that's correct, check that:
- the image itself is actually valid
- its
size
makes sense - it draws sensibly with something like
[image drawInRect:[yourView bounds]]
If all those are so, then you've got an interesting problem. Otherwise, either your image is duff or you're drawing at the wrong time.
The code you posted needs to go in drawRect. Just before drawRect is called, that part of your view is effectively erased, meaning you need to redraw whatever is in the rect that is passed as an argument.
(Note: if you value code simplicity over performance, you can draw the entire view in drawRect rather than only drawing the part that was requested to be drawn.)
精彩评论