Created
September 25, 2013 20:39
-
-
Save bornbnid/6705696 to your computer and use it in GitHub Desktop.
Snippets for code to select image using a UIPopoverController in an iPad app. *Note: you should implement UIActionSheetDelegate and UIImagePickerControllerDelegate
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
#pragma mark - UIAction sheet handling for image options | |
- (IBAction)imageButtonPressed:(id)sender{ | |
UIActionSheet *screenshotOptions = [[UIActionSheet alloc] initWithTitle:@"Image Source" | |
delegate:self | |
cancelButtonTitle:@"Cancel" | |
destructiveButtonTitle:nil | |
otherButtonTitles:@"Camera", @"Photo Library", nil]; | |
[screenshotOptions showFromRect:self.imageButton.frame inView:self.imageButton.superview animated:YES]; | |
} | |
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ | |
if(buttonIndex == 0){ | |
[self getCameraPicture]; | |
}else if(buttonIndex == 1){ | |
[self pickImageFromLibrary]; | |
} | |
} | |
#pragma mark - Image selection handling | |
- (void)getCameraPicture{ | |
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { | |
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; | |
picker.delegate = self; | |
//picker.allowsEditing = YES; | |
picker.sourceType = UIImagePickerControllerSourceTypeCamera; | |
UIPopoverController *newPopoverController = [[UIPopoverController alloc] initWithContentViewController:picker]; | |
self.popover = newPopoverController; | |
[self.popover presentPopoverFromRect:self.imageButton.frame inView:self.imageButton.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; | |
} | |
} | |
- (void)pickImageFromLibrary{ | |
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { | |
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; | |
picker.delegate = self; | |
//picker.allowsEditing = YES; | |
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | |
UIPopoverController *newPopoverController = [[UIPopoverController alloc] initWithContentViewController:picker]; | |
self.popover = newPopoverController; | |
[self.popover presentPopoverFromRect:self.imageButton.frame inView:self.imageButton.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; | |
} | |
} | |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { | |
UIImage *image = info[@"UIImagePickerControllerEditedImage"]; | |
[self.popover dismissPopoverAnimated:YES]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment