Created
March 8, 2014 18:55
-
-
Save ethanmick/9437043 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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