在UIImagePicker中禁用旋转

use*_*887 13 iphone objective-c uiimagepickercontroller ios5

您好我需要禁用UIImagePicker的旋转.
如果用户在iPhone处于"风景"时拍照,则按钮将旋转并且拍摄的照片也将旋转.我想禁用该选项,因此加速度计将无法工作,它将始终像纵向模式一样拍照.
我怎样才能做到这一点?
谢谢,Matan Radomski.

Don*_*mer 28

这是解决方案:[UIDevice endGeneratingDeviceOrientationNotifications].

为什么这么难找?两个原因:

  1. UIDevice保持取向通知多少次打开或关闭计数.如果它已被打开的次数多于关闭时间,则仍会发出通知.
  2. UIImagePickerController开关时,它呈现在这些通知.

因此,调用此方法一次对图像选择器没有任何作用.要确保方向通知已关闭并保持关闭,您需要在选择器出现之前之后关闭它们.

这不会影响iOS或其他应用.它甚至不会完全影响您自己的应用程序:与我建议的其他方法一样,相机按钮会继续响应方向更改,并且拍摄的照片也可以识别方向.这很奇怪,因为如果不需要,应该关闭设备定向硬件.

@try 
{
    // Create a UIImagePicker in camera mode.
    UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease]; 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
    picker.delegate = self; 

    // Prevent the image picker learning about orientation changes by preventing the device from reporting them.
    UIDevice *currentDevice = [UIDevice currentDevice];

    // The device keeps count of orientation requests.  If the count is more than one, it continues detecting them and sending notifications.  So, switch them off repeatedly until the count reaches zero and they are genuinely off.
    // If orientation notifications are on when the view is presented, it may slide on in landscape mode even if the app is entirely portrait.
    // If other parts of the app require orientation notifications, the number "end" messages sent should be counted.  An equal number of "begin" messages should be sent after the image picker ends.
    while ([currentDevice isGeneratingDeviceOrientationNotifications])
        [currentDevice endGeneratingDeviceOrientationNotifications];

    // Display the camera.
    [self presentModalViewController:picker animated:YES];

    // The UIImagePickerController switches on notifications AGAIN when it is presented, so switch them off again.
    while ([currentDevice isGeneratingDeviceOrientationNotifications])
        [currentDevice endGeneratingDeviceOrientationNotifications];
}
@catch (NSException *exception) 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Camera is not available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
Run Code Online (Sandbox Code Playgroud)

如上所述,拍摄的照片可能仍处于错误的方向.如果您希望它们一致地定向,请检查它们的纵横比并相应地旋转.我推荐这个如何旋转UIImage 90度的答案?

//assume that the image is loaded in landscape mode from disk
UIImage * landscapeImage = [UIImage imageNamed: imgname];
UIImage * portraitImage = [[UIImage alloc] initWithCGImage: landscapeImage.CGImage scale:1.0 orientation: UIImageOrientationLeft] autorelease];
Run Code Online (Sandbox Code Playgroud)