Ответ 1
Здесь решение: [UIDevice endGeneratingDeviceOrientationNotifications].
Почему так трудно найти? Две причины:
-
UIDevice
подсчитывает количество включений или отключений ориентации уведомлений. Если он был включен больше времени, чем отключен, уведомления будут выдаваться. -
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];
}
Как отмечено выше, сделанные снимки могут по-прежнему находиться в неправильной ориентации. Если вы хотите, чтобы они были ориентированы последовательно, проверьте их соотношение сторон и повернуть соответственно. Я рекомендую этот ответ: Как повернуть 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];