简体   繁体   English

当方向仅允许横向时,iOS 9 UIImagePickerControllerSourceTypePhotoLibrary崩溃

[英]iOS 9 UIImagePickerControllerSourceTypePhotoLibrary crash when orientation allow only Landscape

I had create app which only support Landscape orientation, whenever I called UIImagePickerControllerSourceTypeCamera to take picture using camera is worked, but when I called UIImagePickerControllerSourceTypePhotoLibrary it crashed. 每当我调用UIImagePickerControllerSourceTypeCamera来使用相机拍照时,我就创建了仅支持横向方向的应用程序,但是当我调用UIImagePickerControllerSourceTypePhotoLibrary时,它崩溃了。

I had tried using method shouldAutoRotate() to allow portrait but still not working. 我曾尝试使用shouldAutoRotate()方法允许肖像,但仍然无法正常工作。 Any solution for this problem ? 这个问题有什么解决办法吗?

As you are getting this error on iPad 当您在iPad上收到此错误时

Try the below code and use UIModalPresentationStyle for this purpose :- 尝试下面的代码,并为此使用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)

This is in Swift, I hope you can covert to objective-c easily. 这是在Swift中,希望您可以轻松地转换到Objective-C。

I hope this helps. 我希望这有帮助。

Set modalPresentationStyle of your imagePickerController to UIModalPresentationCurrentContext and then present it. modalPresentationStyleimagePickerController设置为UIModalPresentationCurrentContext ,然后将其呈现。 Something like, 就像是,

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

Put Presentviewcontroller code in NSOperationQueue 将Presentviewcontroller代码放入NSOperationQueue

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

I give you the solution 我给你解决方案

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM