IOS 横屏打开相册

来源:互联网 发布:谷歌浏览器mac 编辑:程序博客网 时间:2024/05/29 10:51

1.在以往开发中,app调用相册一直没问题。最近开发ipad的时候,项目必须横屏,这时候调用相册的时候app就奔溃了


问题所在: 系统的相册只支持竖屏打开,如果你的app设置了只能横屏,就会冲突,打不开相册,程序会崩溃。


解决方法:

在网上找了很久,很多说在打开相册的时候强制竖屏,或者重写一个类,继承

UIImagePickerController,重写他的

 -(BOOL) shouldAutorotate{

return Yes;

}

-(NSUInteger) supportedInterfaceOrientations{

}

-(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{

}

我也去试了一下,发现并没有什么卵用,都是ios6的,有些方法都弃用了。也许是我的打开方式不对,反正就是没用成功。

后面找了很久,找到一个解决方法,不一定好用,但是确实解决了这个问题。

方法:

1.写了一个管理横屏竖屏的单例类 DeviceDirectionManager

里面有3个方法,(1).是否是横屏。(2).设置横屏.(3).设置竖屏

2.在appDelegate 里面

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{

//判断是否是横屏

    if ( [[DeviceDirectionManager getInstance] isHorizontal]) {

        return UIInterfaceOrientationMaskLandscape;

    }else{

        return UIInterfaceOrientationMaskAll ;

    }

}


3.选择从相册打开

-(void)selectPhotos{

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {


        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        //sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];

        picker.delegate = self;

        picker.allowsEditing = NO;//是否允许编辑

        picker.sourceType = sourceType;

        [self dismissViewControllerAnimated:YES completion:^{

//在打开相册之前,设置屏幕为竖屏

            [[DeviceDirectionManager getInstance] setVertical];

        }];

        [self presentViewController:picker animated:YES completion:nil];

    }

}


4.选择结束或者取消选择的时候,都设置屏幕方向为横屏

    [[DeviceDirectionManager getInstance] setHorizontal];


上传了一个小例子,方法是一样的,命名不一样

链接:http://download.csdn.net/detail/bark_ice/9375688




1 0
原创粉丝点击