views:

89

answers:

4

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 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

+2  A: 

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];
Nicholas M T Elliott
Ok so i make the clock a UIImageView and draw that. I have 1 hand on the clock which i need to rotate around over a 10second period, making that an UIImageView and draw it over the top of the UIImageView that contains the clock would work to create the effect of a little timer clock for my app?
Code
It should do, just make sure the upper UIImageView isn't opaque; upperView.opaque = NO;
Nicholas M T Elliott
+2  A: 

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.

L Cassarani
+1  A: 

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).

Ole Begemann
A: 

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.

Abhijeet Barge