zoukankan      html  css  js  c++  java
  • 关于手机横屏打开相机或者相册闪退解决方案

    今天遇到一个需求就是在手机横屏的时候要打开相册相机,但是在打开的手就报错,经过一上午的查资料,看文档,知道了问题所在,原来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;}
  • 相关阅读:
    剑指offer--26.顺时针打印矩阵
    剑指offer--25.二叉树的镜像
    剑指offer--24.树的子结构
    剑指offer--23.合并两个排序的链表
    剑指offer--22.反转链表
    剑指offer--21.链表中倒数第k个结点
    剑指offer--20.矩形覆盖
    剑指offer--19.重建二叉树
    剑指offer--18.从尾到头打印链表
    剑指offer--17.第一个只出现一次的字符
  • 原文地址:https://www.cnblogs.com/ZhangShengjie/p/6248772.html
Copyright © 2011-2022 走看看