我以这种方式将 UIImagePickerController 添加到我的 View 中:
- (IBAction)TakeCameraShot:(id)sender{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
// Given by default your orientation is in landscaperight already
while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
/* picker.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
picker.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;*/
[self presentViewController:picker animated:YES completion:nil];
/* picker.view.layer.cornerRadius = 150;
picker.view.clipsToBounds = YES;
picker.view.frame = CGRectMake(12, 60, 296, 290);*/
}else{
NSLog(@"Camera is not available");
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Important message" message:@"Unfortunately the camera is not available on your device." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert1 show];
}
}
我试图用这段代码防止它被旋转成横向
// Given by default your orientation is in landscaperight already
while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
提供的堆栈溢出的答案还说,只有当 UIviewController 已经设置为仅纵向时它才会工作,所以这就是我在一般应用程序设置(您选择 ipad、iphone 或 universal)中所做的。但这不起作用。
最佳答案
此代码将在我使用过的 Ipad 上运行,但您需要添加此代码
while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
在呈现 UIImagePickerController
之后 ---
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[self presentViewController:picker animated:YES completion:nil];
while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
这也适用于 ipod,但在 ipod 上它仍然会给你旋转的图像,如果你想在 ipod 中始终使用 potrait 图像,你需要像这样改变它们的方向
UIImageOrientationRight -- 此方向是当相机处于纵向时单击图像时的方向 UIImageOrientationUp——当相机处于横向左侧时
UIImageOrientationDown -- 这是相机横向正确的时候
UIImageOrientationLeft -- 这是相机上下颠倒的时候
现在除了将图像方向更改为 UIImageOrientationRight 之外的所有其他三种情况下。
这可以通过检查即将到来的图像的方向并应用新的方向来完成。
关于ios - 将 UIImagePickerController 修复为纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18854917/