1.在以往開發中,app調用相冊一直沒問題。最近開發ipad的時候,項目必須橫屏,這時候調用相冊的時候app就奔潰了
問題所在: 系統的相冊只支持豎屏打開,如果你的app設置了只能橫屏,就會衝突,打不開相冊,程序會崩潰。
解決方法:
在網上找了很久,很多說在打開相冊的時候強制豎屏,或者重寫一個類,繼承
UIImagePickerController,重寫他的
-(BOOL) shouldAutorotate{
return Yes;
}
-(NSUInteger) supportedInterfaceOrientations{
}
-(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
}
我也去試了一下,發現並沒有什麼卵用,都是ios6的,有些方法都棄用了。也許是我的打開方式不對,反正就是沒用成功。
後面找了很久,找到一個解決方法,不一定好用,但是確實解決了這個問題。
方法:
1.寫了一個管理橫屏豎屏的單例類 DeviceDirectionManager
裏面有3個方法,(1).是否是橫屏。(2).設置橫屏.(3).設置豎屏
2.在appDelegate 裏面
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
//判斷是否是橫屏
if ( [[DeviceDirectionManager getInstance] isHorizontal]) {
return UIInterfaceOrientationMaskLandscape;
}else{
return UIInterfaceOrientationMaskAll ;
}
}
3.選擇從相冊打開
-(void)selectPhotos{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;//是否允許編輯
picker.sourceType = sourceType;
[self dismissViewControllerAnimated:YES completion:^{
//在打開相冊之前,設置屏幕爲豎屏
[[DeviceDirectionManager getInstance] setVertical];
}];
[self presentViewController:picker animated:YES completion:nil];
}
}
4.選擇結束或者取消選擇的時候,都設置屏幕方向爲橫屏
[[DeviceDirectionManager getInstance] setHorizontal];
上傳了一個小例子,方法是一樣的,命名不一樣
鏈接:http://download.csdn.net/detail/bark_ice/9375688