图像旋转AVCam 90度

时间:2012-09-20 12:07:37

标签: iphone ios gpuimage avcam

我在Stackoverflow中查看过很多问题,但没有一个问题解决了我的问题。

所以,我使用Apple的AVCam样本: http://developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html

因此,当我拍照并将其保存在图像库中时,它很好,但是当我通过裁剪在屏幕上显示它并使用

将其发送到服务器时
NSData* pictureData = UIImageJPEGRepresentation(self.snappedPictureView.image, 0.9);

它将它旋转90度!

以下是我裁剪的代码:

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;
}

1 个答案:

答案 0 :(得分:2)

裁剪图像时,您丢失与该图像关联的元数据,告诉它旋转它的正确方法。

相反,您的代码应保留图像的原始旋转,如下所示:

- (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;
}