Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash iPad Photo Picker

I am using the following function to activate either the device camera or the image picker depending on the result of a UIActionSheet. if fromCamera=YES then it works on both iPhone and iPad. if fromCamera=NO then it works on iPhone and the image picker appears. But it crashes on the iPad with the following error: UIStatusBarStyleBlackTranslucent is not available on this device. I already know that the iPad can't display the UIStatusBarStyleBlackTranslucent statusBar, but how do I avoid this crash?

-(void)addPhotoFromCamera:(BOOL)fromCamera{

if(fromCamera){    
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else{
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}


[self presentModalViewController:picker animated:YES];

}

like image 622
wasabi Avatar asked Aug 28 '11 21:08

wasabi


1 Answers

If you set the picker to UIImagePickerControllerSourceTypePhotoLibrary on the iPad, then you must(!) present it in a popoverview, otherwise you get exceptions. I do it like this, to atleast control the size of the popover (the standard size is too small in my opinion):

-(void)openPhotoPicker
{
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.navigationBar.opaque = true;

    //put the image picker in its own container controller, to control its size
    UIViewController *containerController = [[UIViewController alloc] init];
    containerController.contentSizeForViewInPopover = rightPane.frame.size;
    [containerController.view addSubview:imagePicker.view];

    //then, put the container controller in the popover
    popover = [[UIPopoverController alloc] initWithContentViewController:containerController];

    //Actually, I would like to do the following, but iOS doesn't let me:
    //[rightPane addSubview:imagePicker.view];

    //So, put the popover over my rightPane. You might want to change the parameters to suit your needs.
    [popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 10.0,0.0) 
                     inView:rightPane
    permittedArrowDirections:UIPopoverArrowDirectionLeft
                   animated:YES];

    //There seems to be some nasty bug because of the added layer (the container controller), so you need to call this now and each time the view rotates (see below)
    [imagePicker.view setFrame:containerController.view.frame];
}

I also have the following, to counter a rotation bug:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    if(imagePicker!=nil && rightPane.frame.size.width>0)
        [imagePicker.view setFrame:imagePicker.view.superview.frame];
}

It ain't perfect, but it is ok for my testing purposes at the moment. I consider writing my own Imagepicker, because I don't like being forced to use the popoverview... but well, that's a different story.

like image 195
shapecatcher Avatar answered Oct 13 '22 13:10

shapecatcher