Posts Tagged ‘load photo’

Loading images

April 29th, 2009

uiimagepickerThe Photos on every single iPhone / iPod touch can be shared with others apps – even your own. Using UIImagePickerController, you are allowed to access any photo from the device or even take a new one with the camera.

You might have never noticed it, but the same method I’m going to present you, is used in the official Apple built-in applications like Contacts, when you assign a photo to any contact, and Settings while choosing the wallpaper.

This method is not only useful in creating next Photo manager apps  or simple editors.

Before you start you need to add two protocols to your class (header .h) which will handle UIImagePickerController. These two protocols  are: UIImagePickerControllerDelegate and UINavigationControllerDelegate. Your interface code should look like this:

@interface MyView : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {

Once you’ve done that it’s time to implement a method that will show the UIImagePickerController. I use:

 

-(IBAction)openAlbums {
	if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
		UIImagePickerController *UIPicker = [[UIImagePickerController alloc] init];
		UIPicker.allowsImageEditing = NO;
		UIPicker.delegate = self;
		UIPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
		[self presentModalViewController:UIPicker animated:YES];
	}
	else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}

openAlbums method as it’s an IBAction will be called by pressing a button in my case. In bold I highlighted the most important parts. The if statement, checks if asked source type is available (for example, camera is not available on the iPod Touch). If yes, I’m allocating the UIImagePickerController setting delegate, source type, and disable edition, otherwise the alert-error is displayed.

Please note #1: always set to the same property for sourceType and isSourceTypeAvailable. Otherwise, checking if source type is available is pointless.

Please note #2: if you set YES to the property allowsImageEditing, you have the same options of “edition” like in setting the wallpaper, that is resizing and moving.

UIImagePickerControllerSourceTypePhotoLibrary shows you the list of all your albums, which you can browse to choose a photo. There are 2 others source types.

  • UIImagePickerControllerSourceTypeSavedPhotosAlbum
    strictly Camera Roll, where your photos from camera, screenshots, and photos saved by applications are.
  • UIImagePickerControllerSourceTypeCamera
    uses the camera, to take new picture
    Please note: the iPhone simulator can display the camera controller but it won’t use neither iSight nor any other device as a preview. When you take the picture you get the permanent animating activity indicator and you can’t go back, only restart your app.
    simulatorcamera simulatorcameratook

Before you go to the next stage, you should create an UIImageView (IBOutlet UIImageView *imageView;) and place it somewhere on your view, where image you picked, will be displayed. I recommend you to set imageView’s mode property to Aspect Fit to prevent resizing, when the picked image is bigger than imageView’s frame.

Now, you have to implement two more UIImagePickerController methods that will be called when you’ve picked an image or cancel picking.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
	imageView.image = image;
	[[picker parentViewController] dismissModalViewControllerAnimated:YES];
	[picker release];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
	[[picker parentViewController] dismissModalViewControllerAnimated:YES];
	[picker release];
}

The first method is called once you picked a photo using any source type, the second one is called when you cancel picking. Both methods look almost the same – both of them remove the UIImagePickerController by dismissModalViewControllerAnimated:YES and frees the memory. didFinishPickingImage method has one more line of code: imageView.image = image;. It changes the image property of imageView, so from now imageView displays picked image.

Very annoying problem:

pickerproblem

As you see on the example above, status bar is above UIImagePickerController and it doesn’t look good, does it? There are two ways of solving this problem.

  1. Hide the status bar in either the whole your application in the AppDelegate, or if you need it, hide the status bar when you call UIImagePickerController, and show it again in in both two methods: didFinishPickingImage and imagePickerControllerDidCancel.
  2. If you don’t want to use any magic tricks showing and hiding the status bar, in other words you want the status bar to be permanently visible, you are making one mistake since a longer time that has never caused any problems. Your views are of size 320×480px when you only need 320×460px. Go to your view’s size and set the height (H:) to 360. Now in App Delegate where you are adding the view as a window’s subview change the view’s center:
	MyView *viewController = [[MyView alloc] initWithNibName:@"MyView" bundle:[NSBundle mainBundle]];
	[window addSubview:[viewController view]];
	viewController.view.center = CGPointMake(160,250);

Please note: on iPhone Simulator the UIImagePickerController shows immiedietly. On real device it takes few seconds (depending on the size of your photo library) before UIImagePickerController will be shown for the first time.

Here is my project:

xcodeproj

Download the project