Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 239
mooongcle Avatar asked Jan 26 '26 05:01

mooongcle


2 Answers

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

Use [image drawInRect:rect] instead of CGContextDrawImage.

like image 138
David Liu Avatar answered Jan 27 '26 18:01

David Liu


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

like image 34
Shalom Deitch Avatar answered Jan 27 '26 18:01

Shalom Deitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!