Copyright (C) iPhoneアプリ開発備忘録 All rights reserved.
ブログ内で記したコード、内容の正確性は保証いたしません。
記載内容を実装したことにより発生した不具合・損害等の責任は一切負いません。



2015年04月07日

iOS8でカメラ(UIImagePickerController)の画面回転を抑止する



iOS8でUIImagePickerControllerの画面回転を抑止し、Portrait(縦向き)固定にするには、UIImagePickerControllerを継承したサブクラスを作成し、その中で画面回転抑止のコードを記述する。

呼び出し側クラス
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


タグ:Mac apple iPAD IOS iPhone
【UIImagePickerControllerの最新記事】
posted by mobileDeveloper at 19:32 | Comment(0) | TrackBack(0) | UIImagePickerController はてなブックマーク - iOS8でカメラ(UIImagePickerController)の画面回転を抑止する | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック
Apple、Appleのロゴ、App Store、iPodのロゴ、iTunesは、米国および他国のApple Inc.の登録商標です。
iPhone、iPod touch、iPadはApple Inc.の商標です。
iPhone商標は、アイホン株式会社のライセンスに基づき使用されています。
その他、本ブログに記載されている製品名、会社名は、それぞれ各社の商標または登録商標です。
×

この広告は90日以上新しい記事の投稿がないブログに表示されております。