我以这种方式将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或通用)中所做的。但这是行不通的。

最佳答案

该代码可在我使用过的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-这是当相机potrait倒置时

现在除了第一个条件以外,在所有其他三个条件下,都将图像方向更改为UIImageOrientationRight。

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

09-06 06:59