I am trying to develop a simple application which is going to work with the photos the user takes. By following https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Articles/TakingPicturesAndMovies.html I am using a UIImagePickerController in order to take the snapshot. I have a basic application with a single, empty view controller and in the viewDidAppear method I do the following:
- (void)viewDidAppear:(BOOL)animated
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Error" message: #"Can't use the camera!" delegate: nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
[self presentViewController: cameraUI animated: YES completion:NULL];
}
The resulting view becomes something like:
As you can see the front-rear camera change button doesn't show completely and the UIImagePickerController does not fit into the screen. Should I additionally resize before presenting or am I doing something incorrectly?
Related
I'm developing an Application in which I need the UIAlertView to be present in the background while UIImagePickerController is presenting the camera. Currently, the UIAlertView is displayed as a superview (above the camera) when camera controller is presented.
Ideas are appreciated.
AlertViews are added to the window and not the controller. They will always be on the top. you can present the picker in an alertview to overlap it.
you can use this library
https://github.com/wimagguc/ios-custom-alertview
UIImagePickerController *cameraView = [[UIImagePickerController alloc] init];
cameraView.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraView.showsCameraControls = NO;
CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screen.size.width, screen.size.height)];
[container addSubview:cameraView.view];
container.subviews.lastObject.center = CGPointMake(container.subviews.lastObject.center.x, container.center.y);
[alertView setContainerView:container];
[alertView setDelegate:self];
// Display the dialog
[cameraView viewWillAppear:YES]; // trickery to make it show
[cameraView viewDidAppear:YES];
[alertView show];
You can show alertView and then present ImagepickerController. So when you dissmiss that imagePickerController you alertView will there on viewController.
I need help for resolving a warning error. I use a UIImagePickerController and it's showing when I tap on the third tab on my tabBar. When I cancel, I want to show the last tab before the selection of the camera, but when I dismiss my picker, I get a warning!!
This is my code and I don't understand why I get the warning...
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self showPickerController];
}
- (void) showPickerController
{
imagePicker = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
} else{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Erreur !"
message:#"Votre iPhone n'a pas de Camera"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[message show];
}
[self presentViewController:imagePicker animated:NO completion:nil];
}
#pragma mark - Camera Methods
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self.tabBarController dismissViewControllerAnimated:NO completion:^{
if(self.tabBarController!=nil)
[self.tabBarController setSelectedIndex:0];
}];
}
Any idea?
In addition to danh's answer, add a BOOL property to your view controller and add a bit of code to your viewDidAppear: method:
#property (nonatomic) BOOL appeared;
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (!self.appeared || [self isMovingToParentViewController] || [self isBeingPresented]) {
self.appeared = YES;
[self showPickerController];
}
}
This will keep the UIImagePickerController from being immediately presented again after being dismissed.
Also, this isn't really relevant to the question, but you might want to change your showPickerController method a bit to prevent from showing an empty UIImagePickerController if there's no camera available:
- (void)showPickerController
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:NO completion:nil];
}
else {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Erreur !"
message:#"Votre iPhone n'a pas de Camera"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[message show];
}
}
Simple idea: change this to:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self showPickerController];
}
Probably a better idea is to detect the source-type-available condition before beginning the transition to this vc, and instead present the picker vc instead.
I am developing an iOS 7 app in xcode 5. I am using the camera functionality. When the user presses the camera button in my app, I want to give him a choice: take a new picture or choose a picture from the "photostream". I want to show a popup with the choice like "WhatsApp" does, or like the Apple Message app.
I thought; I create an Table View Controller with a section and three cells: take a picture, choose from "photostream" and cancel. I set this section at the bottom of the Table View and make the background transparent. But is does not work ;)
I also searched on Google, but I did not found anything that could help me.
Can anyone tell me how I could do it?
Try this. You have to use UIActionSheetDelegate and UIImagePickerControllerDelegate
- (IBAction)changePhotoButtonPressed:(id)sender {
UIActionSheet *actionSheet=[[UIActionSheet alloc] initWithTitle:#"MY APP"
delegate:self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles:#"Take From Camera",#"Select From Library", #"Cancel", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
actionSheet.destructiveButtonIndex=2;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
}
UIActionSheet Delegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex==0) {
[self takePictureFromCamera];
}
else if (buttonIndex==1) {
[self pickImageFromLibrary];
}
}
Other Methods:
-(void) pickImageFromLibrary
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController* imgPicker=[[UIImagePickerController alloc] init];
self.imagePicker = imgPicker;
//UI Customization
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
[[self.imagePicker navigationBar] setTintColor:[UIColor whiteColor]];
}
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.delegate = self;
[self presentViewController:self.imagePicker animated:YES completion:nil];
} else {
NSLog(#"Attempted to pick an image with illegal source type '%d'", UIImagePickerControllerSourceTypePhotoLibrary);
}
}
-(void) takePictureFromCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController* imgPicker=[[UIImagePickerController alloc] init];
self.imagePicker = imgPicker;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.delegate = self;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
[[self.imagePicker navigationBar] setTintColor:[UIColor whiteColor]];
}
[self presentViewController:self.imagePicker animated:YES completion:nil];
} else {
NSLog(#"Attempted to pick an image with illegal source type '%d'", UIImagePickerControllerSourceTypePhotoLibrary);
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"MY APP" message:#"CAMERA_NOT_AVAILABLE" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
}
You should use an UIImagePickerController and set its type to either UIImagePickerControllerSourceTypeCamera or UIImagePickerControllerSourceTypePhotoLibrary.
And for the popup with the choices, use an UIActionSheet.
You can have a look at this sample code form Apple for the UIImagePickerController:
https://developer.apple.com/library/ios/samplecode/PhotoPicker/Introduction/Intro.html
Background: On iPad, I have a button which when tapped, showed a UIActionSheet. This action sheet has 2 options, camera and gallery. Camera when tapped, pulled up a camera and everything works fine. Gallery when tapped, suppose to show a popover with user's photos in it.
Problem: On iPad, UIActionSheet acts like a popover. Which when presenting, another popover cannot come into view. Error:Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.
My code:
Setting Action Sheet
- (void)imageButtonTapped:(UIButton *)sender
{
if (_commentObject.image){
_actionSheet = [[UIActionSheet alloc] initWithTitle:#"Action" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Remove" otherButtonTitles:nil];
_actionSheet.tag = ACTION_IMAGE_REVIEW_TAG;
}else{
_actionSheet = [[UIActionSheet alloc] initWithTitle:#"Image Source" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Gallery", nil];
_actionSheet.tag = ACTION_IMAGE_SOURCE_TAG;
}
if (_isPad) {
[_actionSheet showFromRect:_imageButton.frame inView:_scrollViewContent animated:YES];
}else{
[_actionSheet showInView:self.view];
}
}
Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (actionSheet.tag) {
case ACTION_IMAGE_SOURCE_TAG:
switch (buttonIndex) {
case 0:
[self pickImage:YES];
break;
case 1:
[self pickImage:NO];
break;
default:
break;
}
break;
}
Executing
- (void)pickImage:(BOOL)fromCamera
{
if (fromCamera) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController* cameraPickerController = [[UIImagePickerController alloc] init];
cameraPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPickerController.delegate = self;
[self presentViewController:cameraPickerController animated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Camera Unavailable" message:#"Your Device does not support Cameras" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
}else{
UIImagePickerController *galleryPickerController = [[UIImagePickerController alloc] init];
galleryPickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
galleryPickerController.delegate = self;
if (_isPad) {
if ([_actionSheet isVisible]) {
[_actionSheet removeFromSuperview];
UIPopoverController *imagePickerPopover = [[UIPopoverController alloc] initWithContentViewController:galleryPickerController];
[imagePickerPopover presentPopoverFromRect:_imageButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}else{
[self presentViewController:galleryPickerController animated:YES completion:nil];
}
}
}
Question: I tried removing the action sheet from view, and tried dismissing it before executing pickImage. None of that works. How do I present the gallery?
Your Problem: Error:Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.
make Object of UIPopoverController in your .h file class. Make sure that your #property for your UIPopoverController is strong instead of weak.
Check this
UIPopoverController: dealloc reached while popover is still visible
UIPopovercontroller dealloc reached while popover is still visible
FYI, This was a comment and OP asked to put it as an answer. Because this solved the issue.
It's happening because you don't have a reference to your UIPopoverController. Have a strong reference for your imagePickerPopover and then try. No problem will occur.
you can try this:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if ([actionSheet isVisible]) {
[actionSheet dismissWithClickedButtonIndex:0 animated:NO];
}
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIImagePickerController *galleryPickerController = ......
}
}
I have an issue with my code, I have two blocks of code of UIAlertViews one with cancel and ok button, and the other to make a UIImagePicker
-(IBAction)publicaPeticion
{
if([txtPeticion hasText] )
{
UIAlertView *alerta = [[UIAlertView alloc]
initWithTitle:#"Confirmación de Compra"
message:#"Deseas comprar la petición por $12.00"
delegate:self
cancelButtonTitle:#"Cancelar"
otherButtonTitles:#"Aceptar", nil];
[alerta show];
}
}
The issue is between publicaPeticion and cargaImagen
-(IBAction)cargaImagen
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Subir una imagen"
message:#"¿De donde deseas subir una imagen?"
delegate:self
cancelButtonTitle:#"Cancelar"
otherButtonTitles:#"Desde el equipo",#"Tomar con camara", nil];
[alert show];
}
and also my method to get the source of the image, from the photo stream or from the camera
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
if(buttonIndex ==2)
{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
}
the question is, that when i press "Aceptar" button (OK), it takes me to the upload picture from the photo library...
maybe is a little bit silly the question but how can I differentiate it?
A couple ways.
1)
See how the delegate method is called?
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
From the alertView parameter, you can tell which alert did the calling (if you set cargaImagen and publicaPeticion's alertviews to separate instance variables).
2)
Another (and probably easier) thing you could do is to set a tag property on your alertView.
In your 'cargaImagen' method and right after you create the UIAlert, set the tag to 1 via alert.tag = 1;.
Then, in your alertView:clickedButtonAtIndex: delegate method, when alertView.tag == 1, you'll know it comes from cargaImagen and if it's 2 (or zero), you know it comes from publicaPeticion.