【发布时间】:2016-04-11 08:13:39
【问题描述】:
我有一个视图控制器,我在其中添加了一个 UIView 全屏大小,在那个 UIView 中我有 AVCapturesession 可以帮助我捕捉照片, 我的视图控制器在纵向模式下打开良好,但在横向模式下突然打开。
代码如下,
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
self.camera.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
pinchGesture.delegate=self;
[self.camera addGestureRecognizer:pinchGesture];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
相机是 UIView,它是我的 UIViewController 的属性,
再次,
-(void) viewDidAppear:(BOOL)animated
{
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
CALayer *viewLayer = self.camera.layer;
NSLog(@"viewLayer = %@", viewLayer);
captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.camera.layer.bounds;
[self.camera.layer addSublayer: captureVideoPreviewLayer];
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];
[session startRunning];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
isUsingFlash = NO;
isUsingFrontFacingCamera = NO;
effectiveScale = 1.0;
}
我的视图在横向模式下打开错误,一旦我旋转到纵向它会很好,再次旋转到横向它工作良好,只是它在横向模式下无法正常启动,为什么?
这里我设置的是根视图控制器,
sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
cameraFirstController = [sb instantiateViewControllerWithIdentifier:@"FirstViewController"];
cameraFirstController.delegate = self;
nav = [[CustomNavigationController alloc]initWithRootViewController:cameraFirstController];
[self.viewController presentViewController:nav animated:YES completion:nil];
【问题讨论】:
-
你能在设置 rootViewController 的地方添加代码吗?
-
我已在您编辑的答案中添加了代码。请检查
标签: ios objective-c uiview autolayout landscape-portrait