如果您的 iOS 应用程序仅处于横向模式,并且您想在应用程序的横向模式下使用相机,那么请尝试以下解决方案。
第 1 步:
在你的 appdelegate.m 类中
-(UIInterfaceOrientationMask)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
NSString *captionVal = [TMUtils getValueInUserDefault:@"CAPTION"];
if ([captionVal isEqualToString:@"Camera"]) {
return UIInterfaceOrientationMaskPortrait;
}else{
return UIInterfaceOrientationMaskLandscapeRight;
}
}else{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
这里您可以将共享偏好值 CAPTION 作为 keyValue 对并存储值“Camera”。
第 2 步:
现在在相机按钮 Action 中的 viewController.m 类中设置共享首选项值并打开将具有相机功能的新 ViewController。
[TMUtils setValueInUserDefault:@"CAPTION" value:@"Camera"];
第 3 步:
在 Camera 功能 viewController.m 类中设置带有 UIImageView 和后退按钮的故事板。
现在在相机功能 viewController.m 的 ViewDidLoad 中设置
- (void)viewDidLoad {
[super viewDidLoad];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"Error");
} else {
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; //this will allow the picker to be presented in landscape
pickerController.delegate = self;
pickerController.allowsEditing = YES;
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:pickerController animated:YES completion:nil];
}
}
现在在 UIImagePickerController 委托方法中将图像设置为 UIImageView
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
self.cameraImage.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
现在在相机背面 UIButton
- (IBAction)cameraBtnAction:(id)sender {
[TMUtils setValueInUserDefault:@"CAPTION" value:@"NOCamera"];
[self dismissViewControllerAnimated:YES completion:nil];
}
它将始终根据委托类函数中的共享首选项值检查supportedInterfaceOrientationsForWindow 参考该值它允许相机功能ViewController以纵向模式打开相机并休息它将再次返回横向模式,这将完全正常工作.