IPhone UIView and UIImagePickerController - iphone

I am new to Iphone development and got a situation where I need to have a ViewController named PhotoSelectorViewController which will ask for one to pick images from phone library. I know UIImagePickerController is there but I don't know how to use it correctly.
I tried following:
First Method:
I created a ViewController named PhotoSelectorViewController with xib file.
I remove View from xib file and added UIImagePickerController to the list.
I don't know what to do next to start the picker when one will create the instance of this class calling some init function in this ViewController.
Second Method:
I created new project and added a view controller named PhotoSelectorViewController where there is a Window and a UIView only.
On UIView viewDidLoad function I am creating instance of UIImagePickerController and adding it to the view.
I don't know how this picker will be started when one will create instance of this view controller?
Please help if you can.

The easiest way is to present it modally from the calling View Controller.
So, for instance, if you have a button on MyController called showPhotos:
-(IBAction)showPhotos:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
Then, you need to implement
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
in this same controller as it (in my example) was set to be the delegate.
allowsEditing allows the user to pan, zoom and crop the image before use. Just set to NO if you don't wish to allow that.
Inside your delegate method, the dictionary contains the resulting UIImage. You can obtain via
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
or
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; if you allow editing.

Your second method is closer, but don't Add it to the view instead you need to either present the UIImagePickerController as a modal view controller, or push it onto a navigation stack.
Think of it as if the PhotoSelectorViewController is a launcher for the UIImagePickerController.
the docs give a nice sequence (see 1-5 near the top): http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html

Related

dismissModalViewAnimated actually removes parent view

I have an application that allows a user to add a picture to a log.
If the user chooses to add from library, everything is fine, but if a user chooses to take
a picture with the camera there's an issue:
When the camera modal view is animated in and I either take a picture and tap on "Use" or i click on the "Cancel" button, the view I'm in when calling dismissModalViewAnimated is removed from its superview.
Anyone got an explanation for this?
Here's the code I use for presenting the modal viewcontroller
pickerCont = [[UIImagePickerController alloc] init];
pickerCont.delegate = self;
pickerCont.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:pickerCont animated:YES];
And this is what i use to dismiss it:
[self dismissModalViewControllerAnimated:YES]
Actually you are dissmissing parentview. here self represents parentView
Use UIImagePickerController's delegate to dissmiss UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//get picked image here
[pickerCont dismissModalViewControllerAnimated:YES]
}

Fitting a UIImagePicker inside a navigation controler?

I'm making an application that uses the iphone camera to detect the changing brightness of the video stream. The video feed should be mid-way through a trail of views under the navigation controller. On pressing a button on this view, the user should see a view that displays the average brightness of the video stream.
I am having quite a lot of trouble getting the UIImagePicker to display in the right way. I've found that it is not possible to output it to a nested UIView - instead, the overlay for the camera should be set to display any extra functionality. I need this overlay to be part of my navigation controller.
I have the following code inside LuxMeterController:
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.tst != YES) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
LuxMeterController* vc = [sb instantiateViewControllerWithIdentifier:#"cameraVc"];
vc.tst = YES;
picker.cameraOverlayView = vc.view;
[self presentModalViewController:picker animated:NO];
}
}
So first, the view is loaded - it initializes the camera and displays it as a modal view. The overlay of the camera is set to a second instance of LuxViewController - this one doesn't render a new camera as tst == YES.
This sort of seems to work - I have a video feed with a full-screen view as its overlay.
My problem is that as the overlay is not part of the hierarchy in the storyboard, it doesn't have UI elements (titlebar, back button,etc) that come from the navigation controller.
How do I get the overlay to sit inside the navigation controller? Or is this the wrong approach to take? How can I get a videostream to sit inside the navigation controller hierarchy, and to have overlay buttons that can move to a view further down the hierarchy?
I've only been doing objective-c and iphone development for a very short period of time, so I might be missing something obvious. If there is a better approach, I'd like to know about it.

UIImagePickerController takePicture not responding

I init the UIImagePickerController:
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
and then use this method [imagePicker takePicture];
and I not get any call to the delegate method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
Any idea why?
To use an image picker controller, you must provide a delegate that conforms to the UIImagePickerControllerDelegate protocol.
if it does, verify that the device is capable of picking content from the desired source. Do this calling the isSourceTypeAvailable: class method, providing a constant from the “UIImagePickerControllerSourceType” enum.
Also check 'self' hasn't been released.
You don't mention if you're actually presenting the image picker controller to the user by calling -presentViewController:animated:completion: (or the older presentViewController:animated:).
Apple's docs don't explicitly say so, but I'm pretty sure the image picker controller needs to be shown on screen in order for -takePicture to work, otherwise you would be able to take pictures without the user knowing.

How to switch views when Iphone camera returns?

I am new to Iphone development. I am working on an application which involves two views. I have a camera button in view one which opens up the default Iphone camera. This is achieved by having this code in the IBAction for camera button in ViewOneController:
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
The view controller for the first view is also the UIImagePickerControllerDelegate for the camera. When the picture is clicked and the camera view returns to the function imagePickerController:didFinishPickingWithMediaInfo where I do this:
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
[self presentModalViewController:ViewTwoViewController animated:YES];
}
So basically all I am trying to achieve is from viewone click "take picture" ---> Open camera --> after camera is done jump to view two. Quite simmilar to what it is in the flickr app. However after I take the picture from camera my app returns to view one and view two is not shown. What am I missing here?
Also from a conceptual perspective I guess IOS keeps a stack of views for any app. When presentModalViewController is called the view is shown and it is added to the stack and when dismissModalViewController is called the view is removed from the stack and the parent view is show. Is that right?
Thanks.
You probably need to put the call to [self presentModalViewController:ViewTwoViewController animated:YES] in viewWillAppear which will be called after the picker view has been removed.
You probably also need to surround the call with some check to only present viewTwo when applicable.

iPhone 3.1 SDK Camera Access

How can I create an application that would initiate a camera and update the image view with the image picked by the camera?
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.
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)