好的,所以下面的代码有效,但我不明白为什么。我正在使用 AVFoundation 从前置摄像头捕获静止图像。在开始捕获之前我有这段代码:
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];
}
然后在captureStillImageAsynchronouslyFromConnection:completionHandler:
中存储图像:
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();
如您所见,我不旋转图像或任何东西,只是在上下文中绘制并保存。但是一旦我尝试使用i
它总是旋转 90 度。如果我尝试使用旋转它
UIImage *rotated = [[UIImage alloc] initWithCGImage:i.CGImage scale:1.0f orientation:i.imageOrientation];
它不起作用(仅使用 没有改变i
)。
我知道 UIImage 可能只是使用正确的方向自动将图像绘制到上下文中,但是 WTF?