Last active
December 19, 2015 05:09
-
-
Save scottsappen/5901883 to your computer and use it in GitHub Desktop.
Using the camera gallery picker or taking a photo on the iPhone in iOS
This file contains hidden or 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
//in your header | |
@property (weak, nonatomic) IBOutlet UIButton *ProfilePictureButton; | |
UIViewController <UIImagePickerControllerDelegate, UIActionSheetDelegate> | |
//in your implementation | |
@synthesize ProfilePictureButton = _profilePictureButton; | |
//implementation details | |
[self setProfilePictureButton:nil]; | |
- (IBAction)ProfilePictureButtonClick:(UIButton *)sender { | |
//If the device has a camera, allow a choice via an action sheet | |
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { | |
NSLog(@"Device has a camera, will show ActionSheet for a choice"); | |
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: nil | |
delegate: self | |
cancelButtonTitle: @"Cancel" | |
destructiveButtonTitle: nil | |
otherButtonTitles: @"Take Photo", | |
@"Choose Existing Photo", nil]; | |
[actionSheet showFromRect: _profilePictureButton.frame inView: _profilePictureButton.superview animated: YES]; | |
} else { | |
NSLog(@"Device does not have a camera, will show gallery"); | |
//otherwise, if it doesn't have a camera, just go ahead and show the picker | |
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; | |
picker.delegate = self; | |
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | |
picker.allowsEditing = YES; | |
[self presentModalViewController:picker animated:YES]; | |
} | |
} | |
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { | |
//Show the picker but only from whatever choice the user made | |
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; | |
picker.delegate = self; | |
picker.allowsEditing = YES; | |
NSString *buttonTitleAtButtonIndex = [actionSheet buttonTitleAtIndex:buttonIndex]; | |
if ([buttonTitleAtButtonIndex isEqualToString:@"Take Photo"]) { | |
// take photo... | |
NSLog(@"User decided to take a photo"); | |
picker.sourceType = UIImagePickerControllerSourceTypeCamera; | |
[self presentModalViewController:picker animated:YES]; | |
} else if ([buttonTitleAtButtonIndex isEqualToString:@"Choose Existing Photo"]) { | |
// choose existing photo... | |
NSLog(@"User decided to pick from the gallery"); | |
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | |
[self presentModalViewController:picker animated:YES]; | |
} | |
} | |
- (void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker { | |
NSLog(@"imagePickerControllerDidCancel called"); | |
[Picker dismissModalViewControllerAnimated:YES]; | |
} | |
- (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info { | |
} | |
- (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info { | |
NSLog(@"didFinishPickingMediaWithInfo called"); | |
UIImage *selectedImage = [info objectForKey:UIImagePickerControllerEditedImage]; | |
//allows editing is TRUE, if you will notice in my picker alloc. | |
//scale box down to 200x200 - reference above kangaroo cropper | |
float actualHeight = 200.0; | |
float actualWidth = 200.0; | |
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight); | |
UIGraphicsBeginImageContext(rect.size); | |
[selectedImage drawInRect:rect]; | |
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
//dismiss the picker | |
[Picker dismissModalViewControllerAnimated:YES]; | |
//get a path to the local documents directory | |
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
NSString *pathToProfilePicture = [NSString pathWithComponents:[NSArray arrayWithObjects:documentsDirectory, @"userprofilepicture", nil]]; | |
//delete the local profile picture, if one exists already | |
BOOL profilePictureFileExists = [[NSFileManager defaultManager] fileExistsAtPath:pathToProfilePicture]; | |
if (profilePictureFileExists) { | |
NSLog(@"User profile picture exists, we will delete it now"); | |
[[NSFileManager defaultManager] removeItemAtPath:pathToProfilePicture error:nil]; | |
} | |
//now create the file | |
BOOL createdFileOk = [[NSFileManager defaultManager] createFileAtPath:pathToProfilePicture contents:nil attributes:nil]; | |
if (!createdFileOk) { | |
NSLog(@"Error creating profile picture image file to write to on disk %@", pathToProfilePicture); | |
} else { | |
NSLog(@"Writing profile picture image file to disk..."); | |
NSFileHandle* profilePictureHandle = [NSFileHandle fileHandleForWritingAtPath:pathToProfilePicture]; | |
[profilePictureHandle writeData:UIImagePNGRepresentation(croppedImage)]; | |
[profilePictureHandle closeFile]; | |
_pictureProfileDirtyBit = @"N"; | |
NSLog(@"Writing profile picture complete"); | |
//show the image you just saved | |
[_profilePictureButton setImage:croppedImage forState:UIControlStateNormal]; | |
//now asynchronously bump this picture up to the web | |
//I might give you the guts of how to upload something like this to a web service listening for such information, but not in this post | |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); | |
dispatch_async(queue, ^{[self uploadMyNewPictureToWeb];}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment