PDA

View Full Version : [iPhone] Drawing images in drawRect




rand0m3r
Jul 3, 2008, 09:35 AM
hi. i've got the following code in my drawRect but nothing is coming out on the screen:

// Drawing the current person's display picture
CGImageRef image = [[appDelegate.people objectAtIndex:appDelegate.currentPersonNum] image2];
CGRect imageRect;
imageRect.origin = CGPointMake(8.0, 8.0);
imageRect.size = CGSizeMake(64.0, 64.0);
CGContextDrawImage(context, imageRect, image);

has anyone had any success with drawing images in your subclassed UIView? cheers.



lucasgladding
Jul 3, 2008, 10:01 PM
My guess is that the problem is in your image2 method. If you are able to post the source code for that, that is the first thing I would try to rule out.

rand0m3r
Jul 4, 2008, 02:00 AM
image2 is simply a CGImageRef property.

firewood
Jul 4, 2008, 03:04 AM
Have you checked to see if your image variable was nil or not?

rand0m3r
Jul 4, 2008, 09:45 PM
u're right, it was null. i've got it work now, but what the hell it's upside down?!

lucasgladding
Jul 4, 2008, 11:28 PM
u're right, it was null. i've got it work now, but what the hell it's upside down?!

Page 38 of the Quartz 2D Programming Guide has one alternative. I'm not sure if isFlipped works when you're working outside the Cocoa drawing methods - you may need to transform the context as mentioned in the guide. If you are new to drawing with Quartz, I would recommend reading the guide before anything else.

rand0m3r
Jul 5, 2008, 05:33 AM
transforming the context is a bad idea becoz everything else will become upside down and only my image will be the right way around.

lucasgladding
Jul 5, 2008, 08:24 AM
transforming the context is a bad idea becoz everything else will become upside down and only my image will be the right way around.

Only if you don't save the graphics state before the transform and restore it afterward.

I haven't done a ton of work with Core Graphics functions, but I have always transformed the matrix before drawing any images. That said, the other drawing has also been done with the transformed matrix. Someone else may chime in with another alternative - I would love to know if there is a better way.

firewood
Jul 5, 2008, 11:30 AM
You could pre-flip your png's offline if you don't want to use affine transforms and save/restore CG state. You'd have to measure the performance hit of the latter.