1

I am developing an iOS 7 app in xcode 5. I am using the camera functionality. When the user presses the camera button in my app, I want to give him a choice: take a new picture or choose a picture from the "photostream". I want to show a popup with the choice like "WhatsApp" does, or like the Apple Message app.

I thought; I create an Table View Controller with a section and three cells: take a picture, choose from "photostream" and cancel. I set this section at the bottom of the Table View and make the background transparent. But is does not work ;)

I also searched on Google, but I did not found anything that could help me.

Can anyone tell me how I could do it?

Tulon
  • 4,011
  • 6
  • 36
  • 56
Marten
  • 1,376
  • 5
  • 28
  • 49
  • Have you looked into `UIImagePickerController` (https://developer.apple.com/library/ios/documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html)? – Popeye May 20 '14 at 12:57
  • Check this they have used [UIActionSheet](https://developer.apple.com/library/ios/documentation/uikit/reference/uiactionsheet_class/Reference/Reference.html) – ChintaN -Maddy- Ramani May 20 '14 at 13:00

2 Answers2

1

Try this. You have to use UIActionSheetDelegate and UIImagePickerControllerDelegate

- (IBAction)changePhotoButtonPressed:(id)sender {
    UIActionSheet *actionSheet=[[UIActionSheet alloc] initWithTitle:@"MY APP"
                                                           delegate:self
                                                  cancelButtonTitle: nil
                                             destructiveButtonTitle: nil
                                                  otherButtonTitles:@"Take From Camera",@"Select From Library", @"Cancel", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    actionSheet.destructiveButtonIndex=2;
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];
}

UIActionSheet Delegate

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex==0) {
        [self takePictureFromCamera];
    }
    else if (buttonIndex==1) {
        [self pickImageFromLibrary];

    }
}

Other Methods:

-(void) pickImageFromLibrary
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        UIImagePickerController* imgPicker=[[UIImagePickerController alloc] init];
        self.imagePicker = imgPicker;
        //UI Customization
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
            [[self.imagePicker navigationBar] setTintColor:[UIColor whiteColor]];
        }

        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        self.imagePicker.delegate = self;

        [self presentViewController:self.imagePicker animated:YES completion:nil];

    } else {
        NSLog(@"Attempted to pick an image with illegal source type '%d'", UIImagePickerControllerSourceTypePhotoLibrary);
    }

}

-(void) takePictureFromCamera
{

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIImagePickerController* imgPicker=[[UIImagePickerController alloc] init];
        self.imagePicker = imgPicker;
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imagePicker.delegate = self;
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
            [[self.imagePicker navigationBar] setTintColor:[UIColor whiteColor]];
        }

        [self presentViewController:self.imagePicker animated:YES completion:nil];

    } else {
        NSLog(@"Attempted to pick an image with illegal source type '%d'", UIImagePickerControllerSourceTypePhotoLibrary);

        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"MY APP" message:@"CAMERA_NOT_AVAILABLE" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    }
}
iBug
  • 2,334
  • 3
  • 32
  • 65
0

You should use an UIImagePickerController and set its type to either UIImagePickerControllerSourceTypeCamera or UIImagePickerControllerSourceTypePhotoLibrary.

And for the popup with the choices, use an UIActionSheet.

You can have a look at this sample code form Apple for the UIImagePickerController: https://developer.apple.com/library/ios/samplecode/PhotoPicker/Introduction/Intro.html