Skip to content

Instantly share code, notes, and snippets.

@calebhicks
Created September 6, 2014 17:54
Show Gist options
  • Save calebhicks/809219798b7ac16f0984 to your computer and use it in GitHub Desktop.
Save calebhicks/809219798b7ac16f0984 to your computer and use it in GitHub Desktop.
image picker sample for ryan
// goes above interface and implementation
typedef NS_ENUM(NSInteger, ActionSheetButton){
ActionSheetFromLibrary,
ActionSheetTakePicture
};
#import <MobileCoreServices/MobileCoreServices.h>
// methods
// this is the code to present the action sheet
- (IBAction)userAvatarTapped:(id)sender {
UIActionSheet *videoActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"From Camera Roll", @"Take Video", nil];
[videoActionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
ActionSheetButton button = buttonIndex;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
NSLog(@"%ld", (long)buttonIndex);
switch (button) {
case ActionSheetFromLibrary:{
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:YES completion:nil];
break;
}
case ActionSheetTakePicture:{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == YES){
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
imagePicker.allowsEditing = YES;
imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
imagePicker.videoQuality = UIImagePickerControllerQualityTypeMedium;
imagePicker.videoMaximumDuration = 6;
[self presentViewController:imagePicker animated:YES completion:nil];
} else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camera Not Available on Device" message:@"This device does not have a camera option. Please choose Photo Library" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
break;
}
}
[self.profileView removeFromSuperview];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Dismiss controller
[picker dismissViewControllerAnimated:YES completion:nil];
// Set Avatar Image
[self.userAvatar setBackgroundImage:cropped forState:UIControlStateNormal];
// Upload image
NSData *imageData = UIImageJPEGRepresentation(cropped, 0.05f);
[self uploadImage:imageData];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment