【发布时间】:2013-09-24 21:34:12
【问题描述】:
好的,所以下面的代码有效,但我不明白为什么。我正在使用 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?
【问题讨论】:
标签: iphone ios objective-c avfoundation ios-camera