0

Can anyone help me to implement a functionality --> Take photo or Choose from gallery options, when camera option is selected in app.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
ragul ml
  • 703
  • 2
  • 7
  • 18

1 Answers1

17

// For taking image by camera

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker animated:YES completion:nil];

// For picking image from gallery

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];

// for output image

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    // output image
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.profileImageView.image = chosenImage;
    [picker dismissViewControllerAnimated:YES completion:nil];

}

Don't forget to add UIImagePickerControllerDelegate and UINavigationControllerDelegate.

Code cracker
  • 3,105
  • 6
  • 37
  • 67
  • I have done this. I want to show the buttons(take picture or use gallery) when camera option is selected. Can u please help me for that. – ragul ml Apr 18 '16 at 07:18
  • 1
    you can use `UIActionsheet` to do that. on your button tap open actionsheet with two options camara or photo library. refer this link http://stackoverflow.com/questions/17223210/creating-uiactionsheet to learn how to use action sheet. open camara on camara button of actionsheet and same for photo library – Ketan Parmar Apr 18 '16 at 07:29
  • you seems to be new iOS developer. just you need to customize by yourself. discuss with your team for that UI. No one can help for this kind of this kind of questions. – Code cracker Apr 18 '16 at 07:44