3

When I am drawing UIImage* image on UIView using the below code, the image is mirrored horizontally. It is like that if I draw 4, it is drawing like this alt text ..

CGRect rect = CGRectMake(x, y, imageWidth, imageHeight);
CGContextDrawImage((CGContextRef) g, rect, ((UIImage*)image).CGImage);

That is the problem??? I am doing wrong??? or if somebody know how to fix it, please let me also know. I very appreciate that in advance.

Thanks a loooooooooot.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
mooongcle
  • 3,987
  • 5
  • 33
  • 42

2 Answers2

5

See: CGContextDrawImage draws image upside down when passed UIImage.CGImage

Use [image drawInRect:rect] instead of CGContextDrawImage.

Community
  • 1
  • 1
David Liu
  • 9,426
  • 5
  • 40
  • 63
  • `[image drawInRect:rect]` will only work if the context is the current graphics context - if it's not, you need to use the Core Graphics methods after all. – Lily Ballard Nov 18 '10 at 22:59
  • True enough, but for doing stuff on UIViews, this is pretty much all you need. Of course, I prefer just using UIImageViews. – David Liu Nov 18 '10 at 23:06
  • I suggest using transformation as described in the latter answer in that question. – tia Nov 18 '10 at 23:07
4

you can turn the picture the right way around using

CGAffineTransform transform =CGAffineTransformMakeTranslation(0.0, height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(context, transform);

//draw image in the context
CGContextDrawImage(context, rect, ((UIImage*)image).CGImage);

Using [image drawInRect:rect] uses the default context i.e. the screen, you can not give it your current context, e.g. if you want to put it as part of a button's ima