UIPopover appearing from wrong button - ios

I have an app which takes an image from existing library, or new from camera.
On the iPad, when I press the 'from library' button the popover appears (correctly) above the button from which it was pressed, however when I press the 'take photo' from the camera button, the camera controller appears over the 'from library' button also... I need this to appear over the 'take photo' button otherwise it looks a bit strange!
here is the code used;
- (void)pickImageFromLibrary: (id)sender
{
[Flurry logEvent: #"PickImage"];
[self openImagePickerWithSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
}
- (void)takePicture: (id)sender
{
[Flurry logEvent: #"TakeImage"];
[self openImagePickerWithSourceType: UIImagePickerControllerSourceTypeCamera];
}
- (void)openImagePickerWithSourceType: (UIImagePickerControllerSourceType)sourceType
{
if ( ![UIImagePickerController isSourceTypeAvailable: sourceType] ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: NSLocalizedString( #"Error", #"" )
message: NSLocalizedString( #"We are sorry, but this functionality is not available at your device.", #"No camera eror" )
delegate: nil
cancelButtonTitle: NSLocalizedString( #"Dismiss", #"")
otherButtonTitles: nil];
[alert show];
return;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = sourceType;
self.isCameraShown = YES;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.popover = [[UIPopoverController alloc] initWithContentViewController:(UIViewController *)picker];
CGRect takePhotoRect;
takePhotoRect.origin = self.view.frame.origin;
takePhotoRect.size.width = 1;
takePhotoRect.size.height = 1;
[self.popover setPopoverContentSize:CGSizeMake(320.0, 216.0)];
[self.popover presentPopoverFromRect:_openLibraryButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else{
[self presentViewController:picker animated:YES completion:NULL ];
}
}

You are presenting the popover from the openLibraryButton.
[self.popover presentPopoverFromRect:_openLibraryButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
You need to add checking to see if the take photo or choose from library button was pressed, then show the popover for that button.
SUDO Code:
if (sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
[self.popover presentPopoverFromRect:_openLibraryButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self.popover presentPopoverFromRect:_takeFromCamera.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

Related

Attempt to present UIImagePickerController on UITabBarController while a presentation is in progress

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.

iOS app with camera popup

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

how to load Video from ipad library in ipad app

I have an iPad app in which when I click on the button it opens a popover with the iPad library to see the photos and pics. I want that when I click the button it should also show video in that but it does not show videos.
In device when I see library it shows videos but when popover opens in app it does not show any video.
UIImagePickerController *pckrImage = [[UIImagePickerController alloc] init];
pckrImage.delegate = self;
if (isiPhone) {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
pckrImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:pckrImage animated:YES];
}
}
else {
popOver = [[UIPopoverController alloc]initWithContentViewController:pckrImage];
[popOver presentPopoverFromRect:CGRectMake(450.0f, 825.0f, 10.0f, 10.0f) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
First Create normal popover then add this code to for button 'choose photo from gallery'
UIImagePickerController *imagePicker1 = [[UIImagePickerController alloc] init];
imagePicker1.contentSizeForViewInPopover=CGSizeMake(300, 200);
imagePicker1.delegate = self;
imagePicker1.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
Popcontroller = [[UIPopoverController alloc] initWithContentViewController:imagePicker1];
[Popcontroller presentPopoverFromRect:cell1.ButtonAddPicture.frame
inView:cell1.ButtonAddPicture.superview
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
and for video add following line to above code
imagePicker1.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];

Displaying ImagePickerController From ActionSheet on iPad

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 = ......
}
}

uipopover issues while click and open the camera for ipad

i have a popoverview issues while click and open the camera for ipad,i wrote code like that
-(IBAction)business_takephotobtnClicked // click the button show the popoverview
{
NSLog(#"business_takephotobtnClicked");
appdelegate.takePhoto=2;
popover = [[UIPopoverController alloc]
initWithContentViewController:imgclass];
popover.popoverContentSize = CGSizeMake(138,66);
[popover presentPopoverFromRect:popbtn_business.bounds inView:popbtn_business
permittedArrowDirections:UIPopoverArrowDirectionUp +
UIPopoverArrowDirectionLeft
animated:YES];
}
-(IBAction) takePhoto:(id)sender // to open the camera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
self.contentSizeForViewInPopover=CGSizeMake(138,66);
UIPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:self.UIPicker animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Camera is not available" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
before click the button the popover like that show
while click the take photo(Neem foto button).THE POPOVERVIEW SIZE IS AUTOMATICALLY EXTENTED like taht
But i need a same size popoverview while open the camera also
Thanks in Advance......
instead of using XIB use to create the camera view programatically and do like the following
-(IBAction)popbtn_Click:(id)sender
{
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 230, 180)];
popoverView.backgroundColor = [UIColor whiteColor];
take_btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[take_btn setTitle:#"Take" forState:UIControlStateNormal];
take_btn.frame=CGRectMake(2,2, 250, 60);
[take_btn addTarget:self action:#selector(take_btnclick:) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:take_btn];
}
-(void)take_btnclick:(id)sender
{
[popoverController dismissPopoverAnimated:YES];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:self.UIPicker animated:YES];
[popoverController dismissPopoverAnimated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Camera is not available" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
if (popoverController != nil)
{
[popoverController dismissPopoverAnimated:YES];
}
}

Resources