iOS-强制横屏状态下打开相册

我们项目iPad版本,视频类,所以强制横屏状态下,但是如果用户想打开相册,上传头像,会导致崩溃。所以做下笔记。

崩溃的原因是:强制横屏状态下,打开相册(相册需要竖屏),所以冲突导致crash,会直接崩到main里。

我的解决办法:

创建一个DJScreenManager单例,并添加横屏、竖屏属性。

------ .h

#import <Foundation/Foundation.h>

@interface DJScreenManager : NSObject

@property (nonatomic,assign) BOOL setVertical;

@property (nonatomic,assign) BOOL setHorizontal;

+ (instancetype)getInstance;


@end

------ .m


#import "DJScreenManager.h"

static DJScreenManager *_screenManager;

@implementation DJScreenManager

+ (instancetype)getInstance{
    
    if (!_screenManager) {
        _screenManager = [[DJScreenManager alloc]init];
    }
    return _screenManager;
}


@end

之后再appdelegate里,根据单例的属性,进行对应的旋转:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if ([DJScreenManager getInstance].setVertical) {
        
        return UIInterfaceOrientationMaskPortrait;
        
    }else{
        
        return UIInterfaceOrientationMaskLandscapeLeft;
        
    }
}

最后,只需要在打开相册的时候,设置竖屏,选取完或者取消的时候,设置回横屏就ok了:

- (IBAction)changeUserAvatar:(UIButton *)sender {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择图片" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
//    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIImagePickerController *vc = [[UIImagePickerController alloc]init];
    vc.delegate = self;
    vc.allowsEditing = YES;
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            
            [vc setSourceType:UIImagePickerControllerSourceTypeCamera];
            [self presentViewController:vc animated:YES completion:nil];
            
        }];
        [alert addAction:cameraAction];
    }
    
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        
        [vc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:vc animated:YES completion:nil];
        
    }];
    
//    [alert addAction:cancel];
    [alert addAction:albumAction];
    
    [DJScreenManager getInstance].setVertical = YES;
    [DJScreenManager getInstance].setHorizontal = NO;
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

选取完或者取消:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0){
    
  //  NSData *data = UIImagePNGRepresentation(image);
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    self.imageData = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    self.userImage.image = [UIImage imageWithData:data];
    
    [DJScreenManager getInstance].setVertical = NO;
    [DJScreenManager getInstance].setHorizontal = YES;
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
//相机或相册的取消代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [DJScreenManager getInstance].setVertical = NO;
    [DJScreenManager getInstance].setHorizontal = YES;
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,287评论 25 708
  • 1,NSObject中description属性的意义,它可以重写吗?答案:每当 NSLog(@"")函数中出现 ...
    eightzg阅读 4,190评论 2 19
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,232评论 4 61
  • 感慨你妈逼啊一个个的,该没钱还是没钱,该丑还是丑,该你妈的苦逼依旧苦逼,咸鱼也配有梦想?
    來点酒阅读 135评论 0 1
  • 在我们编写Restful Api的时候,非常常见的是flask内建的exception不够用,所以我们需要扩展fl...
    troywinter24阅读 624评论 0 0