問題:最近在iPad開發中遇到的一個調用相冊的橫豎屏問題,正常調用相冊不管iPad是橫豎屏,開啟的相冊都是豎屏的---不多說直接上代碼
解決:給UIImagePickerViewController寫一個category 實現以下代碼
#import<UIKit/UIKit.h>
@interface UIImagePickerController (MyImagePicker)
- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
@end
#import "UIImagePickerController+MyImagePicker.h"
@implementation UIImagePickerController (MyImagePicker)
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
@end
然后-->開啟相冊的時候通知Appdelegate,實現UIInterfaceOrientationMask=UIInterfaceOrientationMaskAll 如下代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_myInterfaceOrientation = UIInterfaceOrientationMaskLandscape;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeDirection:) name:@"changeDirection" object:nil];
return YES;
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)changeDirection:(NSNotification *)notice{
if ([notice.object isEqualToString:@"0"]) {
_myInterfaceOrientation = UIInterfaceOrientationMaskLandscapeRight;
}else{
_myInterfaceOrientation = UIInterfaceOrientationMaskAll;
}
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return _myInterfaceOrientation;
}
最后-->(代碼思路,如果有相同之處,不是故意為之)以下是demo地址:demo