在iOS上旋转AVFoundation剧照时的奇怪行为

时间:2013-09-19 17:00:13

标签: iphone ios objective-c avfoundation ios-camera

好的,所以下面的代码有效,但我不明白为什么。我正在使用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?

1 个答案:

答案 0 :(得分:0)

您可以像这样检查设备方向:

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

如果符合上述条件或条件满足,则可以使用以下代码旋转图像:

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

}