mooongcle
mooongcle

Reputation: 4097

Image drawing problem when using CGContext. Image is mirrored horizontally

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.

Upvotes: 3

Views: 7586

Answers (2)

Shalom Deitch
Shalom Deitch

Reputation: 71

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

Upvotes: 4

David Liu
David Liu

Reputation: 9600

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

Use [image drawInRect:rect] instead of CGContextDrawImage.

Upvotes: 5

Related Questions