关于手机横屏打开相机或者相册闪退解决方案

今天遇到一个需求就是在手机横屏的时候要打开相册相机,但是在打开的手就报错,经过一上午的查资料,看文档,知道了问题所在,原来UIImagePickerController 只支持竖屏

 解决思路

1,让UIImagePickerController 支持横屏

2 ,在打开相机的时候让项目横竖屏,在关闭相机或者相册的时候还原 让项目只支持横屏。

3 在appdelegate 通过通知来切换屏幕的横竖屏 - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ return _MyInterfaceOrientationMask; }

 appdelegate里面
@property (nonatomic,assign) NSInteger MyInterfaceOrientationMask;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    _MyInterfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRotate:) name:@"changeRotate" object:nil];
    
    return YES;
}
- (void)changeRotate:(NSNotification *)noti{
    if ([noti.object isEqualToString:@"0"]) {
        _MyInterfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;
    }else{
        _MyInterfaceOrientationMask = UIInterfaceOrientationMaskAll;
    }
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return _MyInterfaceOrientationMask;
}
view里面 
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRotate" object:@"1"];
    ownPickViewController *picker = [[ownPickViewController alloc] init];
    picker.delegate = self;
    if (buttonIndex==0) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    }
    else if(buttonIndex==1) {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRotate" object:@"0"];
    [picker dismissModalViewControllerAnimated:NO];
}
//实现图片选择器代理

//参数:图片选择器  字典参数

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRotate" object:@"0"];
    //通过key值获取到图片
    
    UIImage * image =info[UIImagePickerControllerOriginalImage];
    
    NSLog(@"image=%@  info=%@",image, info);
    
    //判断数据源类型
    
    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
        
        
        
    }
    
}

重新的 UIImagePickerController  里面加一个 
-(BOOL)shouldAutorotate{ return YES;}

 

posted @ 2017-01-04 15:10  ZhangShengjie  阅读(2147)  评论(0编辑  收藏  举报