Gold challenge image is inverted by default


I am trying the Gold Challenge in this chapter, and when I render the icon.png image, it is inverted (the hat is upside down) when I run from the simulator. I verified that when rendered as an icon, it is the right side up. Also, when I view the icon.png by double clicking from project navigator, it is upright as well. My code is as follows:

-(void)drawRect:(CGRect)dirtyRect { CGContextRef ctx = UIGraphicsGetCurrentContext(); UIImage *image = [UIImage imageNamed:@"icon.png"]; CGRect logoBounds = CGRectMake(0, 0, 50, 50); CGContextDrawImage(ctx, logoBounds, [image CGImage] );

Assuming this is expected behavior (please let me know why), I tried to rotate it using imageWithCGImage:scale:orientation - but whatever value I pass for orientation, I get the same inverted image. An example code (I have tried rotating left here, but all the values of orientation give the same inverted image result).

CGContextRef ctx = UIGraphicsGetCurrentContext();
// CGRect bounds = [self bounds];

UIImage *image = [UIImage imageNamed:@"icon.png"];
CGRect logoBounds = CGRectMake(0, 0, 50, 50);
UIImage *rotatedImage = [UIImage imageWithCGImage:[image CGImage] scale:1.0 
CGContextDrawImage(ctx, logoBounds, [rotatedImage CGImage] );


Update: I found a way to make it draw right side up - from viewtopic.php?f=218&t=4120
Instead of CGContextDrawImage, I call [image drawInRect:logoBounds]. I still don’t understand why this works while the other one didn’t.

Update2: I read through the guides on Quartz programming and graphics, and I understand now. The coordinate system used by the CG (Quartz) APIs is different from those used by the UIKit (UI) apis - which is why the image shows inverted while using the CG API in drawRect (which is a UIKit call).


Thanks for posting updates to your original question. I reached the same solution but again wondered why it was being inverted and thought to check in case I wasn’t the first.