简体   繁体   中英

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.

I had tried using method shouldAutoRotate() to allow portrait but still not working. Any solution for this problem ?

As you are getting this error on iPad

Try the below code and use UIModalPresentationStyle for this purpose :-

                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.

I hope this helps.

Set modalPresentationStyle of your imagePickerController to UIModalPresentationCurrentContext and then present it. Something like,

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

Put Presentviewcontroller code in 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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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