1

I am using AV Foundation for a photo capture app. After the user takes a photo, I set my main layer's sublayer's contents property to equal the captured photo aka imageData:

subLayer.contents = (id)[UIImage imageWithData:imageData].CGImage;

This all works perfectly and the photo does display on the screen.

The only problem is the photo displays rotated at 90 degrees.

Any idea how I can display the photo correctly instead of being rotated?

Here is how the photo looks when the user is capturing it:

enter image description here

But then here is how it looks when I display it as the sublayer:

enter image description here

user3117509
  • 833
  • 3
  • 14
  • 32

3 Answers3

2
UIImage *img = [UIImage imageWithData:imageData];
    UIImage *image_Corrected;
    if (img.imageOrientation !=UIImageOrientationUp)
    {

        image_Corrected=[UIImage imageWithCGImage:img.CGImage scale:0.0 orientation:UIImageOrientationUp];
    }
    else
    {
        image_Corrected=img;

    }
hmdeep
  • 2,910
  • 3
  • 14
  • 22
1

Given an image that displays rotated when set like this:

subLayer.contents = (id)[UIImage imageWithData:imageData].CGImage;

... you'll find that this works okay:

UIImageView *view = [UIImageView initWithImage:[UIImage imageWithData:imageData];
// then add view and display

UIImage, or classes that use UIImage, comprehend the metadata that may effect display of an image, like rotation.

Because you are accessing the underlying Quartz image data directly, you are skipping the part where the UIImage displays rotated data. So, either attempt to do this another way (e.g. set a background image with UIImage) or rotate the CGImage yourself (with the various CG functions).

greymouser
  • 3,133
  • 19
  • 22
  • Could you please provide some example code for: "rotate the CGImage yourself (with the various CG functions)"? I've been trying now for the last 20 minutes and just can't get it to change it's orientation. – user3117509 Feb 20 '14 at 04:27
  • There are about a million ways to skin a cat, when it comes to Core Graphics. Basically you should get the orientation from the original UIImage, use CGAffineTransformMakeRotation to make a transform to rotate it correctly, create a bitmap context, apply the transform and save the context. Rotating goes hand in hand with scaling, so if you need to scale, you can use a transform for that, too. You can also get the CIImage from the original, apply some CIFilters to rotate and scale, and get a new CGImage from that. This is straightforward, but non-trivial -- check the docs! – greymouser Feb 20 '14 at 04:34
  • thanks for the detailed reply. I ended up using this solution here: http://stackoverflow.com/a/10548409/3117509 ...The only problem now is the image is stretched and distorted. I am using an AVCaptureVideoPreviewLayer and have it set to only take up half the screen. Is it possible that the camera is capturing the whole screen instead of just the half? – user3117509 Feb 20 '14 at 04:39
1

The camera always take pictures in the same orientation, it's a software duty to rotate it or change its EXIF metadata dictionary according to the current orientation.
There are some subtle difference between how this can be handled if you use still image capture or the output data buffer.
I wrote an article on my blog about it: EXIF pain
I suggest you also to check that sample code from Apple site, probably is one of the most complete with AVCam.

Andrea
  • 26,120
  • 10
  • 85
  • 131