iOS 9 UIImagePickerControllerSourceTypePhotoLibrary 在方向只允许横向时崩溃

iOS 9 UIImagePickerControllerSourceTypePhotoLibrary crash when orientation allow only Landscape

我创建了只支持横向的应用程序,每当我调用 UIImagePickerControllerSourceTypeCamera 以使用相机拍照时都可以,但是当我调用 UIImagePickerControllerSourceTypePhotoLibrary 时它崩溃了。

我曾尝试使用方法 shouldAutoRotate() 允许纵向但仍然无法正常工作。这个问题有什么解决办法吗?

因为您在 iPad

上收到此错误

试试下面的代码并为此目的使用 UIModalPresentationStyle :-

                self.imagepicker.modalPresentationStyle = UIModalPresentationStyle.Popover
                let popover = self.imagepicker.popoverPresentationController
                self.imagepicker.preferredContentSize = CGSizeMake(400 ,400)
                popover!.sourceView = self.view
                popover!.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)
                popover!.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
                self.presentViewController(self.imagepicker, animated: true, completion: nil)

这是在Swift,希望你能轻松变身到objective-c。

希望对您有所帮助。

将你的imagePickerControllermodalPresentationStyle设置为UIModalPresentationCurrentContext然后出示。像,

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;

将Presentviewcontroller代码放入NSOperationQueue

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.presentViewController(self.imagepicker, animated: true, completion: nil)
    }];

我给你解决方案

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.wantsFullScreenLayout = YES;

    UIPopoverController *imagePopover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    [imagePopover setDelegate:self];
    [imagePopover setPopoverContentSize:CGSizeMake(320, 500) animated:NO];  //set your content size
    [imagePopover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];   //I give presentPopoverFromRect as button frame.Change here what you want

}