原因大致是由于UIImagePickerController默认支持的是竖屏的原因,实现原理在打开相册前设置屏幕可翻转,关闭相册后设置为横屏模式
解决步骤
1,为 UIImagePickerController 添加类别如下
@interface UIImagePickerController (LandScapeImagePicker)
- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
@end
#import "UIImagePickerController+LandScapeImagePicker.h"
@implementation UIImagePickerController (LandScapeImagePicker)
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
returnUIInterfaceOrientationMaskAll;
}
@end
同时在打开相册控制类中添加如下控制代码
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
bAlbumListViewStatus = NO;
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
//[self uploadImageWithImage:image];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
bAlbumListViewStatus = NO;
[picker dismissViewControllerAnimated:NO completion:nil];
}
弹出相册的地方代码修改如下
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = sourceType;
picker.allowsEditing = YES;
bAlbumListViewStatus = YES; //
[self presentViewController:picker animated:YES completion:nil];
2,在app delegate里添加方向支持
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (bAlbumListViewStatus) //全局变量,定义是否打开相册
return UIInterfaceOrientationMaskAll;
else
return UIInterfaceOrientationMaskLandscapeLeft;
}
完成如上后即可解决了