At the moment i have declared my UIImage.
I load in an image into it.
But when I call the draw method I see nothing appear on the screen. Trying 开发者_Python百科to get a little clock image to appear on my View.
clockImage = [ UIImage imageWithContentsOfFile:@"clock.png"];
[clockImage drawAtPoint:(CGPointMake(300.0, 300.0))];
Am i missing some vital steps? Is there more setup required?
Many Thanks, Code
The problem you're having is caused by the fact that drawAtPoint:
is only meant to be used inside a valid graphics context, usually in the drawRect:
method of a UIView
(which sets up a CGContext
for you corresponding to the device screen). If you decide to use this approach, remember to load the image somewhere other than the view's drawRect:
method, to avoid having to perform such a memory-intensive operation whenever your view needs to be drawn to screen.
On the other hand, if you simply need to display an image on the screen, your best bet is to look at UIImageView
, which will do most of the heavy lifting for you.
Generally you'll create a UIImageView and load your image into that, in order to persist the image on the screen. Otherwise you'll be required to repaint your UIImage whenever the OS demands it.
UIImageView* view = [[UIImageView alloc] initWithImage:
[UIImage imageWithContentsOfFile:@"clock.png"]];
[self.view addSubView:view];
Where are you placing this code? drawAtPoint:
will draw into the currently active graphics context and will fail if not graphics context has been created. You will have to place the drawAtPoint:
line into drawRect:
(where a graphics context is already created).
I think you are missing correct path for image,
if i am thinking right then try this -
`NSString *filePath = [[NSBundle mainBundle] pathForResource:@"clock" ofType:@"png"];
clockImage = [ UIImage imageWithContentsOfFile:filePath]; [clockImage drawAtPoint:(CGPointMake(300.0, 300.0))];`
hope this will help you.
精彩评论