How do I use UIImagePickerController just to display the camera and not take a picture?

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2010-04-08T14:32:34Z Indexed on 2010/05/25 9:21 UTC
Read the original article Hit count: 522

Hello All:

I'd like to know how to open the camera inside of a pre-defined frame (not the entire screen). When the view loads, I have a box, and inside it, I want to display what the camera sees. I don't want to snap a picture, just basically use the camera as a viewfinder. I have searched this site and have not yet found what I'm looking for. Please help.

Thanks!
Thomas

Update 1:

Here is what I have tried so far.

1.) I added UIImageView to my xib.
2.) Connect the following outlet to the UIImageView in IB

IBOutlet UIImageView *cameraWindow;

3.) I put the following code in viewWillAppear

-(void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];
 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
 picker.delegate = self;

 picker.sourceType = UIImagePickerControllerSourceTypeCamera;
 [self presentModalViewController:picker animated:YES];

 NSLog(@"viewWillAppear ran");
}

But this method does not run, as evident by the absence of NSLog statement from my console. Please help!

Thanks,
Thomas


Update 2:

OK I got it to run by putting the code in viewDidLoad but my camera still doesn't show up...any suggestions? Anyone....? I've been reading the UIImagePickerController class reference, but am kinda unsure how to make sense of it. I'm still learning iPhone, so it's a bit of a struggle. Please help!

- (void)viewDidLoad 
{ 
 [super viewDidLoad]; 

 // Create a bool variable "camera" and call isSourceTypeAvailable to see if camera exists on device
 BOOL camera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

 // If there is a camera, then display the world throught the viewfinder
 if(camera)
 { 
  UIImagePickerController *picker = [[UIImagePickerController alloc] init];

  // Since I'm not actually taking a picture, is a delegate function necessary?
  picker.delegate = self;

  picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  [self presentModalViewController:picker animated:YES];

  NSLog(@"Camera is available");
 }

 // Otherwise, do nothing.
 else 
  NSLog(@"No camera available");
}

Thanks!
Thomas


Update 3:

A-HA! Found this on the Apple Class Reference.

Discussion

The delegate receives notifications when the user picks an image or movie, or exits the picker interface. The delegate also decides when to dismiss the picker interface, so you must provide a delegate to use a picker. If this property is nil, the picker is dismissed immediately if you try to show it.

Gonna play around with the delegate now. Then I'm going to read on wtf a delegate is. Backwards? Whatever :-p


Update 4:

The two delegate functions for the class are – imagePickerController:didFinishPickingMediaWithInfo: – imagePickerControllerDidCancel:

and since I don't actually want to pick an image or give the user the option to cancel, I am just defining the methods. They should never run though....I think.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone-sdk