There are several ways to let the user select an image from a iphone or an ipad. All what we have to do is to use the UIImagePickerController to start the image picking task from the Photo Library, Saved Photos Album and the Camera.
In this post I'm going to explain how to pick an image from the Photo Library. So, make sure that you have images in your photo Library.
Note that you have to tell the program header file (your .h file) that you are using the following delegates
UIApplicationDelegate
UIImagePickerControllerDelegate
UINavigationControllerDelegate
All what you have to do is adding a button on your screen and let the user click on it to start popping up the image picker, let them select an image. If an image is selected successfully, then automatically the delegate method didFinishPickingMediaWithInfo will be called where you can view your image in a UIImageView or do whatever the processing you want with the image.
-(IBAction)selectImage:(id)sender
{
[self selectAnImage];
}
//method to let the user select an image from the UIImagePickerView
-(void)selectAnImage
{
ipc=[[UIImagePickerController alloc] init];//instantiate a UIImagePickerViewController object
ipc.delegate=self;
//First I am checking that the camera is available
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
NSLog(@"Camera is available");
ipc.sourceType=UIImagePickerControllerSourceTypeCamera;
}
//if not check whether the saved photo album is available
else if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
NSLog(@"Camera is not available");
NSLog(@"Photo Album is available");
ipc.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
//if none of the above two are avaialble, then use the Photo Library
else
{
NSLog(@"Camera is not available");
NSLog(@"Photo Album is not available");
NSLog(@"Photo Library is available");
ipc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:ipc animated:YES];
}
//ImagePickerControler delegate
//triggers when the user has selected an image
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"Test.txt"];
NSString *temp=[[NSString alloc]initWithString:@"Sample String"];
[temp writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//set the selected image
self.sourceImage=[info objectForKey:UIImagePickerControllerOriginalImage];
imageDisplay.image=self.sourceImage;
[self checkImageResolution:self.sourceImage];
[[picker parentViewController]dismissModalViewControllerAnimated:YES];
[picker dismissModalViewControllerAnimated:YES];
}
//ImagePickerControler delegate
//triggers when user did cancel the image selection from the UIimagePickerView
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController]dismissModalViewControllerAnimated:YES];
}
No comments:
Post a Comment