Skip to content

Instantly share code, notes, and snippets.

@ethanmick
Created March 8, 2014 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethanmick/9437043 to your computer and use it in GitHub Desktop.
Save ethanmick/9437043 to your computer and use it in GitHub Desktop.
- (IBAction)action:(UIButton *)sender;
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Take Picture", @"Choose from Library", nil];
[sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
switch (buttonIndex) {
case 0: //Take Picture
{
[self startCameraControllerFromViewController:self usingDelegate:self];
break;
}
case 1: //Choose From Library
{
[self startMediaBrowserFromViewController:self usingDelegate:self];
break;
}
default: // 2 = cancel
break;
}
}
#pragma mark - Image Picking/Taking
- (BOOL)startCameraControllerFromViewController:(UIViewController *)controller
usingDelegate:(id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate;
{
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}
- (BOOL)startMediaBrowserFromViewController:(UIViewController *)controller
usingDelegate:(id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)delegate;
{
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// Displays saved pictures and movies, if both are available, from the
// Camera Roll album.
mediaUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
mediaUI.allowsEditing = NO;
mediaUI.delegate = delegate;
[controller presentViewController:mediaUI animated:YES completion:nil];
return YES;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
self.profileImage = image;
if (image) {
[self.profileImageViewButton setImage:image forState:UIControlStateNormal];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment