1. Create a new UIViewController subclass
Create UIViewController subclass, I named it TestViewController here, and Add the following code to AppDelegate.
* AppDelegate.h
@interface TestViewController : UIViewController
{
UIImageView *imageView;
}
- (void)buttonClicked:(id)sender;
@end
* AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
testViewController = [[TestViewController alloc] init];
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
You must need ‘AssetsLibrary.framework’ and ‘MobileCoreServices.framework’. Add the frameworks into your project.
2. Modify TestViewController
TestViewController.h
@interface TestViewController : UIViewController
{
UIImageView *imageView;
}
- (void)buttonClicked:(id)sender;
@end
TestViewController.m
- (void)buttonClicked:(UIButton *)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = (sender.tag == 1) ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera;
if (sender.tag == 2) { // Camera
NSString *requiredMediaType = (NSString *)kUTTypeImage;
imagePicker.mediaTypes = [NSArray arrayWithObject:requiredMediaType];
}
imagePicker.delegate = self;
self presentModalViewController:imagePicker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = img;
[picker dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 390)];
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
UIButton *pickButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[pickButton setTitle:@"Pick" forState:UIControlStateNormal];
pickButton.frame = CGRectMake(50, 410, 100, 40);
pickButton.tag = 1;
[pickButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pickButton];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIButton *takeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[takeButton setTitle:@"Take" forState:UIControlStateNormal];
takeButton.frame = CGRectMake(170, 410, 100, 40);
takeButton.tag = 2;
[takeButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:takeButton];
}
}
if you want to save the image from camera to Photo Library, add following code in ‘imagePickerController:didFinishPickingMediaWithInfo:’ method.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = img;
ALAssetsLibraryWriteImageCompletionBlock completeBlock = ^(NSURL *assetURL, NSError *error) {
if (error == nil) {
NSLog(@"Image Path:%@", [assetURL absoluteString]);
}
};
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
if (url == nil) {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[img CGImage] orientation:(ALAssetOrientation)[img imageOrientation] completionBlock:completeBlock];
}
[picker dismissModalViewControllerAnimated:YES];
}
Recent Comments