I have troubles using UIImagePicker to take pictures based on the "Taking Pictures with the Camera" chapter from the iPhone programming guide.
Whatever i try all i get is a grey/white screen.
here is my code :
- (void)viewDidLoad {
[super viewDidLoad];
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES];
}
I've tried to call the imagePicker from the viewDidLoad, viewWillAppear or awakeFromNib, nothing changes.
My viewController implements UINavigationControllerDelegate and UIImagePickerControllerDelegate.
I'm running this on OS3.1 on a first gen iPhone.
Any help would be greatly appreciated !
Thanks,
Vincent.
I would placing that call in viewDidAppear as oposed to viewDidLoad.
Related
I am trying to show a UIImagePickerController from a button click. When I click the button, I get a SIGABRT at the line:
[self presentModalViewController:camera animated:YES];
from the code block:
camera = [[UIImagePickerController alloc]init];
[camera setSourceType:UIImagePickerControllerSourceTypeCamera];
[camera setDelegate:self.view];
camera.showsCameraControls = NO;
camera.navigationBarHidden = YES;
camera.wantsFullScreenLayout = YES;
camera.toolbarHidden = YES;
camera.cameraOverlayView = bottomArrow;
[self presentModalViewController:camera animated:YES];
where camera is the name of the UIImagePickerController defined as such:
UIImagePickerController *camera;
in the #interface.
My interface declaration is:
#interface cameraViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
Can someone see what I'm doing wrong?
Besides the good point made by #Vikings, always check if your device has a camera before trying to use it:
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[camera setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[camera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
Make sure you are using the both the Navigation Controller Delegate and the Image Picker Controller Delegate. The Image Picker is actually a Navigation Controller, which is why you have to implement its delegate.
#interface YourViewController : UITableViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
Also, set the delegate correctly, not to the view, but the View Controller.
camera.delegate = self;
The delegate needs to be set to the View Controller, and not the View Controller's View.
Check out the code below:
(1) You do not need to hide the navigation bar, because there is not one
(2) You do not need to hide the toolbar, because there is not one
(3) You do not need to specify wantsFullScreenLayout, because a Modal View Controller will always take up the full screen
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.showsCameraControls = NO;
// Comment out the line below to make sure it is not causing a problem.
// This just expects a view, so if bottomArrow is a view you should be fine
picker.cameraOverlayView = bottomArrow;
[self presentModalViewController:picker animated:YES];
Also, I did not realize you were loading this code in viewDidLoad, this will crash, because the View Controller itself is not finished it's transition, so you cannot begin another transition. Instead use viewDidAppear for the same effect:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
// Place code here
}
Do not put your code in viewDidLoad create one IBAction and put your code inside that function.
and set delegate to your view controller. and in .h file make sure that your wrote
<UIImagePickerControllerDelegate>
[camera setDelegate : self];
i'm using UIImagePickerControllerSourceTypeCamera in my app, but a want to add the camera view a subview. not as modal view.
now i'm using
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
i want to use it like this, but doesn't seem to work ..
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[picker.view setFrame:CGRectMake(300, 40, 300, 200)];
[self.view addSubview:picker.view];
i know that the UIImagePickerControllerSourceTypeCamera only can be used as modal view.
How is for example Hipstamatic using this for example?
This is possible in iOS 4+ using the AVCaptureSession framework. It's much more complicated than using UIImagePickerController, but you have complete control over input and output. The key to having the camera view as a subview is the following code. Assume you have a subview called previewView that you want to show the camera view:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
previewLayer.frame = self.previewView.bounds;
[self.previewView.layer addSublayer:previewLayer];
There is more setup work to do to give your AVCaptureSession object the correct input and output devices and to actually capture the photos. That can be found in the documentation and examples on the web.
You're right, the only supported methods for displaying a UIImagePickerController seem to be presentModalViewController:animated: or UIPopoverController.
I'm not familiar with Hipstamatic, but chances are it is using AVFoundation, in particular AVCaptureVideoPreviewLayer.
Visit link to get idea how things can be accomplished. Look out for AVCam Demo in developer.apple.com resources for IOS programme. You will definitely get idea how it works. You should be registered developer with APPLE, and thats very easy.
Hope it helps.
Is it possible to pop the camera (using UIImagePickerControllerSourceTypeCamera) on the iPad 2 inside of a UIPopoverController using presentModalViewController?
Whenever I do it the camera just goes full screen. I need it inside of the UIPopoverController.
This won't work by calling presentModalViewController:animated:. To make the camera show up in a UIPopoverController's UI, then you want something similar to the following:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
UIPopoverController *popover = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
[popover presentPopoverFromBarButtonItem:launchCameraButton
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
There is another way to launch the popover from an arbitrary spot in your UI also. The docs should explain that.
I have a UIImagePickerController that is presented as a modal view controller. This works fine, except for when I try to add anything to the camera overlay view.
As soon as I modify the camera overlay view, the default zoom control stops working (although tap to focus still works). If I tap on the view, the zoom slider appears, but I am not able to slide it up and down like usual.
Here's the code I'm using:
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsEditing = NO;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls = YES;
imagePickerController.wantsFullScreenLayout = NO;
[imagePickerController.cameraOverlayView addSubview:overlayView];
[self presentModalViewController:imagePickerController animated:YES];
Has anyone had a similar problem or can see what I'm doing wrong? I know it should work, as I've seen it on other apps that also use the default camera controls.
Thanks :)
After a lot of mucking round I've finally come across a solution. Don't use the cameraOverlayView property, instead add a subview to the UIImagePickerController with a frame of size CGRectZero.
In the following example, overlayView is a custom subclass of UIView which draws an image and passes touches through to the next responder:
OverlayView *overlayView = [[OverlayView alloc] initWithFrame:CGRectZero];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsEditing = NO;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls = YES;
[imagePickerController.view addSubview:overlayView];
statViewController.imagePickerController = imagePickerController;
}
-
And then, everything just works. Not sure if adding a subview to UIImagePickerController is documented and ok with Apple, but heaps of other apps use this (or a similar workaround) so shouldn't cause any submission problems.
I'm just playing around with a simple program that opens the camera. That's literally all that I want to do. I'm a beginner and I believe that I have the basics down in terms of UI management for the iPhone so I decided to give this one a whirl.
What I'm trying to do right now is...
- (BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
UIImagePickerController * camera = [[UIImagePickerController alloc] init];
camera.delegate = self;
camera.sourceType = UIImagePickerControllerSourceTypeCamera;
camera.allowsEditing = NO;
camera.showsCameraControls = NO;
[viewController presentModalViewController:camera animated:NO];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES:
}
So basically, initialize the camera, set some things and show it in the main view. I set the camera's delegate to self because this code is placed in the delegate class (and yes, the delegate class is conforming to UIImagePickerControllerDelegate && UINavigationControllerDelegate).
Main problem right now is that nothing is appearing on the screen. I have absolutely no idea what I'm doing wrong, especially since the program is building correctly with no errors or warnings...
Any help is appreciated! Thanks a lot :D
You are trying to display the UIImagePickerController from your view controller before you'e added its view to the window. That won't work. It has to be presented from a view that is already displaying.
I'm not absolutely sure this will work as I haven't tried it, but switch the order of your code. Add your view controller to the window first:
- (BOOL) application:(UIApplication*) application
didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
[window addSubview:viewController.view];
[window makeKeyAndVisible];
UIImagePickerController * camera = [[UIImagePickerController alloc] init];
camera.delegate = self;
camera.sourceType = UIImagePickerControllerSourceTypeCamera;
camera.allowsEditing = NO;
camera.showsCameraControls = NO;
[viewController presentModalViewController:camera animated:NO];
return YES:
}
If this doesn't work, then try moving your UIImagePickerController code to your -viewDidLoad of your root view controller (whatever viewController is in your code).