Last active
December 19, 2015 19:39
-
-
Save richardleggett/6007835 to your computer and use it in GitHub Desktop.
Image picker (camera and Photo Library) snippet
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
- (void)photoViewTapHandler:(UITapGestureRecognizer *)gesture { | |
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Picture Selection" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take a photo", @"Choose from Photo Library", nil]; | |
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque; | |
[popupQuery showInView:self.view]; | |
} | |
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { | |
if (buttonIndex == 0) { | |
[self showCamera]; | |
} else if(buttonIndex == 1) { | |
[self showPhotoPicker]; | |
} | |
} | |
- (void)showCamera { | |
_imagePicker = [[UIImagePickerController alloc] init]; | |
_imagePicker.delegate = self; | |
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; | |
_imagePicker.allowsEditing = YES; | |
[self presentViewController:_imagePicker animated:YES completion:nil]; | |
} | |
- (void)showPhotoPicker { | |
_imagePicker = [[UIImagePickerController alloc] init]; | |
_imagePicker.delegate = self; | |
_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | |
_imagePicker.allowsEditing = YES; | |
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { | |
_imagePickerPopover = [[UIPopoverController alloc] initWithContentViewController:_imagePicker]; | |
[_imagePickerPopover presentPopoverFromRect:self.photoView.frame | |
inView:self.view | |
permittedArrowDirections:UIPopoverArrowDirectionAny | |
animated:YES]; | |
} else { | |
[self presentViewController:_imagePicker animated:YES completion:nil]; | |
} | |
} | |
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { | |
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && | |
picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) { | |
[_imagePickerPopover dismissPopoverAnimated:YES]; | |
} else { | |
[self dismissViewControllerAnimated:YES completion:nil]; | |
} | |
} | |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { | |
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && | |
picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) { | |
[_imagePickerPopover dismissPopoverAnimated:YES]; | |
} else { | |
[self dismissViewControllerAnimated:YES completion:nil]; | |
} | |
NSLog(@"Picked: %@", info); | |
UIImage *selectedImage; | |
UIImage *editedImage = [info valueForKey:UIImagePickerControllerOriginalImage]; | |
if(editedImage != nil) { | |
selectedImage = editedImage; | |
} else { | |
selectedImage = [info valueForKey:UIImagePickerControllerOriginalImage]; | |
} | |
//save the image to app directory | |
NSData *pngData = UIImagePNGRepresentation(selectedImage); | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *folderPath = [paths objectAtIndex:0]; | |
NSString *filePath = [folderPath stringByAppendingPathComponent:@"avatar.png"]; | |
if([pngData writeToFile:filePath atomically:YES]) { | |
// set the image | |
[self.photoView setImage:[UIImage imageWithContentsOfFile:filePath]]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment