
1. 在过去的开发中,应用程序调用相册一直没有问题. 最近开发iPad时,该项目必须是水平的,并且在调用专辑时该应用程序崩溃了.
问题: 系统的相册仅支持垂直屏幕打开. 如果您的应用设置为仅水平屏幕,则会发生冲突. 如果无法打开相册,则程序将崩溃.
解决方案:

在互联网上寻找了很长时间,很说打开专辑时,有必要强制垂直屏幕,或者重写一个类,继承
UIImagePickerControllerios 横屏打开相册,重写他的
-(BOOL) shouldAutorotate{
return Yes;
}
-(NSUInteger) supportedInterfaceOrientations{
}
-(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
}

我也尝试过,发现没有鸡蛋,所有都是ios6,并且不赞成使用某些方法. 也许我的打开方法是错误的,无论如何,它是没有用的.
看了很久以后,发现了一个不一定很容易使用的解决方案,但确实解决了问题.
方法:

1. 我写了一个Singleton类DeviceDirectionManager来管理水平和垂直屏幕
其中包含3种方法,(1). 无论是风景还是非风景. (2). 设置水平屏幕. (3). 设置垂直屏幕
2. 在appDelegate内部

-(NSUInteger)应用程序: (UIApplication *)应用程序受支持InterfaceOrientationsForWindow: (UIWindow *)窗口
{
//判断是否是横屏
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. 完成选择或取消选择后ios 横屏打开相册,屏幕方向将设置为横向
[[DeviceDirectionManager getInstance] setHorizontal];
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-220946-1.html
第二