Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone modal dialog like native "take picture, choose existing"

Tags:

iphone

ios4

How do I create a modal dialog with customisable button choices just like the "take picture or video" | "choose existing" dialog on the iPhone? Those buttons aren't normal UIButton's and I'm sure they aren't hand crafted for every app out there.

like image 738
Mike S Avatar asked Apr 21 '11 03:04

Mike S


Video Answer


1 Answers

It sounds like you want to use a combination of UIActionSheet and UIImagePickerController. This code shows a popup that lets the user choose to take a photo or choose an existing one, then the UIImagePickerController pretty much does everything else:

- (IBAction)handleUploadPhotoTouch:(id)sender {
    mediaPicker = [[UIImagePickerController alloc] init];
    [mediaPicker setDelegate:self];
    mediaPicker.allowsEditing = YES;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                 delegate:self
                                                        cancelButtonTitle:@"Cancel"
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:@"Take photo", @"Choose Existing", nil];
        [actionSheet showInView:self.view];
    } else {
        mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;     
        [self presentModalViewController:mediaPicker animated:YES];
    }
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        mediaPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    } else if (buttonIndex == 1) {
        mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;     
    }

    [self presentModalViewController:mediaPicker animated:YES];
    [actionSheet release];
}

NOTE: This assumes you have a member variable "mediaPicker" which keeps a reference to the UIImagePickerController

like image 156
Chris R Avatar answered Oct 19 '22 23:10

Chris R