Last active
August 29, 2015 14:05
-
-
Save imack/3ee413ab5805a1008a51 to your computer and use it in GitHub Desktop.
UIImagePicker Example
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
#import <UIKit/UIKit.h> | |
@interface CreateImageViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate> | |
@property(nonatomic, strong) IBOutlet UIImageView *imageView; | |
@end |
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
@implementation CreateImageViewController | |
@synthesize imageView; | |
-(void) viewDidAppear:(BOOL)animated{ | |
[super viewDidAppear:animated]; | |
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){ | |
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Photo Library", nil]; | |
actionSheet.tag = 0; | |
[actionSheet showInView:self.view]; | |
}else { | |
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; | |
imagePicker.delegate = self; | |
imagePicker.allowsEditing = true; | |
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; | |
[self presentViewController:imagePicker animated:true completion:nil]; | |
} | |
} | |
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { | |
switch (buttonIndex) { | |
case 0:{ | |
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; | |
imagePicker.delegate = self; | |
imagePicker.allowsEditing = true; | |
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera]; | |
[self presentViewController:imagePicker animated:true completion:nil]; | |
} | |
break; | |
case 1: { | |
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; | |
imagePicker.delegate = self; | |
imagePicker.allowsEditing = true; | |
//Use photo library instead of camera roll so people can pick photos from other albums | |
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; | |
[self presentViewController:imagePicker animated:true completion:nil]; | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { | |
UIImage *selectedImage = [info objectForKey:UIImagePickerControllerEditedImage]; | |
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; | |
CGRect cropRect = [[info objectForKey:@"UIImagePickerControllerCropRect"]CGRectValue]; | |
if(!selectedImage) selectedImage = originalImage; | |
if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) { | |
// Do something with an image from the camera | |
UIImageWriteToSavedPhotosAlbum(originalImage, nil, nil, nil); | |
} | |
[self.imageView setContentMode:UIViewContentModeScaleAspectFit]; | |
self.imageView.image = [selectedImage resizedImageByMagick: @"612x612"]; | |
[picker dismissViewControllerAnimated:YES completion:NULL]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment