好的,我已经成功解决了这个问题。需要注意的是 UIImagePickerController 类仅支持 Apple 的纵向模式文档 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html.
要捕获旋转willRotateToInterfaceOrientation
在这里没用,所以你必须使用通知。此外,在运行时设置自动布局约束也不是可行的方法。
在应用程序委托中didFinishLaunchingWithOptions
您需要启用轮换通知:
// send notification on rotation
[[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];
In viewDidLoad
相机OverlayView的方法UIViewController
添加以下内容:
//add observer for the rotation notification
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
最后添加orientationChanged:
相机叠加的方法UIViewController
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
double rotation = 0;
switch (orientation) {
case UIDeviceOrientationPortrait:
rotation = 0;
break;
case UIDeviceOrientationPortraitUpsideDown:
rotation = M_PI;
break;
case UIDeviceOrientationLandscapeLeft:
rotation = M_PI_2;
break;
case UIDeviceOrientationLandscapeRight:
rotation = -M_PI_2;
break;
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationUnknown:
default:
return;
}
CGAffineTransform transform = CGAffineTransformMakeRotation(rotation);
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.btnCancel.transform = transform;
self.btnSnap.transform = transform;
}completion:nil];
}
上面的代码将旋转变换应用于我在本例中使用的 2 个 UIButtons btnCancel 和 btnSnap。这将为您提供旋转设备时的相机应用程序效果。
我仍然在控制台中收到警告<Error>: CGAffineTransformInvert: singular matrix.
不知道为什么会发生这种情况,但这与相机视图有关。
希望以上内容有所帮助。