Reputation: 4980
I'm writing an application that allows users to upload photos from their camera roll and save them in the app. The only problem is that I'm not sure where to begin. I read over Apples documents on UIImageView, but that didn't seem to help as much as it usually does. I was wondering if anyone can point me in the right direction of some sample code that does generally the same thing?
Thank you!
Upvotes: 1
Views: 1679
Reputation: 1844
Well let's clear things up first
Now let's see how it actually comes together:
First you'll want to create an UIImagePickerController, define the source of the images and set a delegate to handle the image that comes back. Something along these lines:
// Always make sure to test if the source you want is available
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
//Our image picker is ready - now let's show it to the user
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
This will bring up the image picker to the user ( specifically a camera - you can play with the media types and sources - lookup the UIImagePickerController Reference to see what other options you have.
Now - you have to actually take care of the image you get from the user and that's done through the delegate method implementation:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//All the info from the image - including the image itself - are stored in the info dictionary.
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//Now - we have a reference to the image as a UIImage, so we can display it in a UIImageView or save it to disk or upload to a webserver. To show it - just set your imageView's image to the image
imageView.image = image;
}
}
Upvotes: 2