美文网首页
iOS-强制横屏状态下打开相册

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

作者: 伦敦乡下的小作家 | 来源:发表于2017-06-13 11:23 被阅读251次

我们项目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];
}

相关文章

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

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

  • iOS:强制横屏的坑

    前段时间我们播放器强制横屏,项目设置允许竖屏,在手机不锁屏状态下,手机横屏会导致播放器强制横屏的时候会导致横屏失败...

  • iOS强制横屏

    iOS强制横屏

  • 强制横屏方法

    强制横屏: 方法一: 关于强制横屏看了很多文章,首先第一个方法是invocation,这个方法可以实现横屏效果,但...

  • 强制横屏

    转载自 :http://blog.csdn.net/zhaotao0617/article/details/525...

  • 强制横屏

    -(void)landscapeAndPortraitChange{UIInterfaceOrientation ...

  • 强制横屏

    1、AppDelegate里设置属性 @property(nonatomic,assign)BOOLallowLa...

  • 横屏开启相册及iOS10权限相关设置

    iOS横屏开发如何开启本地相册 为什么说横屏如何开启相册呢,开启相册不就是启动UIImagePickerContr...

  • iOS强制横屏方法之一

    //这段代码是强制产生横屏效果,通过kvo实现//强制右横屏 可以过审核-(void)viewWillAppea...

  • 横竖屏

    需求: 让push的ViewController界面强制横屏 一、配置 二、添加强制横屏方法 pragma mar...

网友评论

      本文标题:iOS-强制横屏状态下打开相册

      本文链接:https://www.haomeiwen.com/subject/akrdqxtx.html