views:

1590

answers:

4

I have a question about camera access. And it's simply, how can I create an application that would initiate a camera and update de image view with the image picked by the camera?

I am really a noob on camera handling, done about a tutorial or two, but couldn't manage to create this simple app, for study reasons. Can anyone give a light on that?

+1  A: 

Did you check this out?

http://stackoverflow.com/questions/74113/access-the-camera-with-iphone-sdk

nall
A: 

Yes I did check, still I could not get it to work... I did something like:

-viewDidLoad()...
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = pickerDelegate
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

self.view = picker.cameraOverlayView;
}

Still, I got nothing.

Marcelo Roque
That's not even valid Objective-C code. Also, this belongs as a _comment_ to nall's answer.
Ben Alpert
A: 

If you think about it, self.view = picker.cameraOverlayView just copies an empty transparent view over you own!! It doesn't even add it to the screen, not that it would work...

Instead, you need to present the picker controller:

[self.navigationController presentModalViewController:picker animated:YES];

Then make sure to implement the delegate calls (which it looks like you may have since you already set the delegate)

Kendall Helmstetter Gelner
A: 

Try this out.

In the viewDidLoad method, initialize the UIImagePickerController, assign its property sourceType as UIImagePickerControllerSourceTypeCamera and delegate as self.

Define a button in your view controller where on its click event get the modal view of the imagepicker, like:

[self presentModalViewController:self.picker animated:YES];

here picker is the object of UIImagePickerController.

Next,implement the didFinishPickingMediaWithInfo delegate of UIImagePickerController. In this delegate, you can assign a UIImage to the Dictionary object info, then save the image to your local instance of UIImageView(its ImageToSave below)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 

[[picker parentViewController] dismissModalViewControllerAnimated:YES];
UIImage *img = [info objectForKey:@"UIImagePickerControllerImage"];
ImageToSave.image = img;

}

Do not forget to include the UIImagePickerControllerDelegate into your main view controller's .h file.

See if this works or not.

CodeWriter