Pergunta

I've looked through tons of questions here in Stackoverflow and none of them solved my problem.

So, I'm using Apple's AVCam sample: http://developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html

So, when I take a picture and save it in the image library it's fine, but when I show it in the screen by cropping it and when I send it to a server by using

NSData* pictureData = UIImageJPEGRepresentation(self.snappedPictureView.image, 0.9);

it sends it 90 degrees rotated!

Here is the code I crop it:

UIImage* cropped = [image imageByCroppingRect:CGRectMake(0, 0, (image.size.width *             300)/self.view.frame.size.width, (image.size.height * 300)/self.view.frame.size.height)];


imageByCroppingRect is:
- (UIImage *) imageByCroppingRect:(CGRect)area
{
UIImage *croppedImage;
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], area);
// or use the UIImage wherever you like
croppedImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

return croppedImage;
}
Foi útil?

Solução

When you crop the image, you are loosing the metadata associated with that image telling it the proper way to rotate it.

Instead your code should preserve the original rotation of the image like this:

- (UIImage *) imageByCroppingRect:(CGRect)rect {

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(imageRef);
    return result;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top