将UIImagePickerController修复为纵向

时间:2013-09-17 16:19:17

标签: ios

我以这种方式将UIImagePickerController添加到我的视图中:

- (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或通用)所做的。但这不起作用。

2 个答案:

答案 0 :(得分:1)

此代码适用于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 - 此方向是在相机处于potrait时单击图像的方向    UIImageOrientationUp - 当景观中的相机离开时

UIImageOrientationDown - 这是相机正确的风景

UIImageOrientationLeft - 这是相机是potraitupside down

现在除了首先将图像方向更改为UIImageOrientationRight之外的所有其他三个条件。

这可以通过检查即将到来的图像的方向并应用新的方向来完成。

答案 1 :(得分:0)

只需为UIImagePickerController创建一个类别,并将这行代码放在该类

  • (BOOL)shouldAutorotate { 返回NO; }