0
votes

In the application I'm working at you can take a picture with the iPad camera. After that using CoreGraphics you can draw shapes on that image.
At first the image was upside down and mirrored. I resolved that with this:

CGContextTranslateCTM(myContext, 0, backgroundImage.size.height);
CGContextScaleCTM(myContext, 1.0, -1.0);

But now when you take the image in portrait mode, the imported image is rotated to the left (so it's presented horizontally). I rotated the image back with this code:

UIImage *tempImage = [[UIImage alloc] initWithCGImage:imagetest.CGImage];

CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, 0, tempImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
CGContextRef ctx = CGBitmapContextCreate(NULL, tempImage.size.width, tempImage.size.height,
                                         CGImageGetBitsPerComponent(tempImage.CGImage), 0,
                                         CGImageGetColorSpace(tempImage.CGImage),
                                         CGImageGetBitmapInfo(tempImage.CGImage));
CGContextConcatCTM(ctx, transform);
CGContextDrawImage(ctx, CGRectMake(0,0,tempImage.size.height,tempImage.size.width), tempImage.CGImage);
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);

Now the image is shown in the right way (portrait), but I can't draw properly on it, maybe because the width and height are reversed.

From what I read there is a meta tag with the image orientation that cannot be read by CoreGraphics.

Do you know a better way to rotate the image? Or any solution that would keep the image from rotating when taking a photo in portrait mode?

2

2 Answers

0
votes

Yes that is an issue because default orientation of device camera is Landscape, if you take picture in portrait mode and see preview in Photo Gallery it will be fine, but as you use it in your app it will be rotated 90 Degrees, to fix that issue i have written answer in my Recent Post Here

0
votes

If you tell the image to draw itself, it will respect its own orientation. No need to flip it (it does that itself) and no need to rotate it.