Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keicoder/9681278 to your computer and use it in GitHub Desktop.
Save keicoder/9681278 to your computer and use it in GitHub Desktop.
objective-c : dealing with UIImagePickerController and UIActionSheet
//dealing with UIImagePickerController and UIActionSheet
//ImageViewController.h
@interface ImageViewController : UIViewController
@end
//ImageViewController.m
@interface ImageViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>
@property (nonatomic, weak) IBOutlet UIImageView * imageView;
@property (nonatomic, weak) IBOutlet UILabel *imageLabel;
@end
@implementation ImageViewController
{
UIImagePickerController *_imagePicker; //이미지 피커
UIActionSheet *_actionSheet; //show Photo Menu
UIImage *_image;
}
#pragma mark - View Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageLabel.text = @"Image shows here";
}
#pragma mark - ImagePicker Controller
- (IBAction)showPhotoMenu:(id)sender
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
_actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Take Photo", @"Choose From Library", nil];
[_actionSheet showInView:self.view];
} else {
[self choosePhotoFromLibrary];
}
}
- (void)takePhoto
{
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.delegate = self;
_imagePicker.allowsEditing = YES;
[self presentViewController:_imagePicker animated:YES completion:nil];
}
- (void)choosePhotoFromLibrary
{
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_imagePicker.delegate = self;
_imagePicker.allowsEditing = YES;
[self presentViewController:_imagePicker animated:YES completion:nil];
}
#pragma mark - Image
- (void)showImage:(UIImage *)image
{
self.imageView.image = image;
self.imageView.hidden = NO;
self.imageLabel.hidden = YES;
}
#pragma mark - UIImagePickerController Delegate
//must conform to both UIImagePickerControllerDelegate and UINavigationControllerDelegate
//but don’t have to implement any of the UINavigationControllerDelegate methods.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
_image = info[UIImagePickerControllerEditedImage];
[self showImage:_image];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
[self takePhoto];
} else if (buttonIndex == 1) {
[self choosePhotoFromLibrary];
}
_actionSheet = nil;
}
#pragma mark - Dealloc
- (void)dealloc
{
NSLog(@"dealloc %@", self);
}
#pragma mark - Memory Warning
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment