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

2017-06-13  本文已影响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];
}
上一篇下一篇

猜你喜欢

热点阅读