0

OK, so the following code works, but I don't get why. I am capturing still images from the Front camera using AVFoundation. I have this code before initiating capture:

if ([connection isVideoOrientationSupported]) {
    AVCaptureVideoOrientation orientation;

    switch ([UIDevice currentDevice].orientation) {
        case UIDeviceOrientationPortraitUpsideDown:
            orientation = AVCaptureVideoOrientationPortraitUpsideDown;
            break;
        case UIDeviceOrientationLandscapeLeft:
            orientation = AVCaptureVideoOrientationLandscapeRight;
            break;
        case UIDeviceOrientationLandscapeRight:
            orientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        default:
            orientation = AVCaptureVideoOrientationPortrait;
            break;
    }

    [connection setVideoOrientation:orientation];
}

and then this in the captureStillImageAsynchronouslyFromConnection:completionHandler: to store the image:

NSData *imageData = [AVCaptureStillImageOutputjpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *i = [UIImage imageWithData:imageData];
orientation:i.imageOrientation];

UIGraphicsBeginImageContext(i.size);

[i drawAtPoint:CGPointMake(0.0, 0.0)];

image.image = UIGraphicsGetImageFromCurrentImageContext();

as you can see, I don't rotate the image or anything, just draw it in the context and save. But as soon as I try to use i it is always rotated by 90 degrees. If I try to rotate it using

UIImage *rotated = [[UIImage alloc] initWithCGImage:i.CGImage scale:1.0f orientation:i.imageOrientation];

it doesn't work (no change from just using i).

I understand that UIImage might just draw the image into the context using the right orientation automatically, but WTF?

mirosval
  • 6,671
  • 3
  • 32
  • 46

1 Answers1

0

you can check the device orientation like this :

 if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft && position ==      AVCaptureDevicePositionBack)
                                                                    )
                                                             {

and if above condition or whatever your condition is satisfies then you can rotate your image using the code below :

-(void)rotateImageByDegress:(int)degrees{
if (degrees == 0.0) {
    return self;
}
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
[rotatedViewBox release];

// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();

// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

//   // Rotate the image context
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));

// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;

}

Yasir Ayaz
  • 11
  • 3