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

今天遇到一个需求就是在手机横屏的时候要打开相册相机,但是在打开的手就报错,经过一上午的查资料,看文档,知道了问题所在,原来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 @   ZhangShengjie  阅读(2181)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示