呼び出し側クラス
CameraViewController.m
#import "CameraSubViewController.h"
- (void) cameraView {
CameraSubViewController* imagePicker = [[CameraSubViewController alloc] init];
[imagePicker autorelease];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
// イメージピッカーを表示する
[self presentViewController: imagePicker animated:YES completion: nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// イメージピッカーを隠して画面回転の抑止を解除する
[picker dismissViewControllerAnimated:YES completion:^{
UIDevice *currentDevice = [UIDevice currentDevice];
while (![currentDevice isGeneratingDeviceOrientationNotifications])
[currentDevice beginGeneratingDeviceOrientationNotifications];
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
// イメージピッカーを隠して画面回転の抑止を解除する
[picker dismissViewControllerAnimated:YES completion:^{
UIDevice *currentDevice = [UIDevice currentDevice];
while (![currentDevice isGeneratingDeviceOrientationNotifications])
[currentDevice beginGeneratingDeviceOrientationNotifications];
}];
}
UIImagePickerControllerを継承したサブクラス
CameraSubViewController.h
#import
@interface CameraSubViewController : UIImagePickerController
@end
CameraSubViewController.m
#import "CameraSubViewController.h"
@interface CameraSubViewController ()
@end
@implementation CameraSubViewController
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
// 画面回転を抑止する
UIDevice *currentDevice = [UIDevice currentDevice];
while ([currentDevice isGeneratingDeviceOrientationNotifications])
[currentDevice endGeneratingDeviceOrientationNotifications];
return UIInterfaceOrientationMaskPortrait;
}
@end
参考記事はiOS8より前のコード例だが、このままiOS8向けのアプリに記述しても動作しない。
UIImagePickerControllerを継承したサブクラスに画面回転を抑止するコードを記述すると動作するようになる。
参考記事;
http://stackoverflow.com/questions/10073483/disable-rotation-in-uiimagepicker
http://captainshadow.hatenablog.com/entry/20130210/1360503249
http://qiita.com/satoshi0212/items/764469910d63b5eedc9a
【UIImagePickerControllerの最新記事】