Camera keeps opening onViewDidAppear..how can I stop this? - objective-c

I have an app with a camera. The user presses a button and the view changes to the camera view.
In the cameraView, I put the pickerControl in the viewDidLoad.
When the view appears, the camera opens and allows the user to take a picture. When done, the user can choose to keep the photo and if he does, the camera view dismisses and in that moment, I can see the picture I've taken in the image viws I created but the camera keeps opening up.
I think it's because I have my pickerControl in my viewDidLoad and every time the camera dismisses, because of that, it repeatedly keeps opening the camera. Is there any way around this?
Here is my cameraViewController that loads when the user selects the camera option
- (void)viewDidLoad
{
[super viewDidLoad];
}
// When the view appears, the camera will turn on
-(void)viewDidAppear:(BOOL)animated
{
// Initialize the picker control
UIImagePickerController *pickerControl = [[UIImagePickerController alloc] init];
// If all's good, start up the camera
if ((pickerControl != nil)&&(picHolder == nil))
{
pickerControl.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerControl.delegate = self;
pickerControl.allowsEditing = true;
}
// Camera turns on
[self presentViewController:pickerControl animated:true completion:nil];
}
Im not sure how to prevent the view from loading every time when that is what opens the camera in the first place.
Thank You

Add a BOOL ivar to your class. Some pseudocode below
YourController {
BOOL didShow;
}
-(void)viewDidAppear:(BOOL)animated {
if (!didShow) {
// present the UIImagePickerController
}
didShow = YES;
}

Related

Tap and hold to save image in UIWebview

In my app I have a UIWebView that shows a list of pictures. Right now, when someone taps and holds their finger on a picture, the option comes up to copy that picture.
Is there a way to make it so that, when someone taps and holds, the option to save that picture appears?
Any ideas are welcomed!
You need to detect the long tap. For that you need to add:
#property (nonatomic,strong) UILongPressGestureRecognizer *lpgr;
and in your view did load:
self.lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPressGestures:)];
self.lpgr.minimumPressDuration = 1.0f;
self.lpgr.allowableMovement = 100.0f;
[self.view addGestureRecognizer:self.lpgr];
and ask the user to save the picture once the app detects the long tap.
- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender
{
if ([sender isEqual:self.lpgr]) {
if (sender.state == UIGestureRecognizerStateBegan)
{
//prompt the user to save the picture .
}
}
}

Remove RevealViewController PanGestureRecognizer from Login Screen

I have implemented SidebarDemo project from Appcode and then modified Storyboard with Login screen, now when I start my application Login Screen come which is correct and I dont want Pan Gesture on Login screen and I am trying to remove it from code below
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.view removeGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
for (UIGestureRecognizer *recognizer in self.navigationController.view.gestureRecognizers) {
[self.navigationController.view removeGestureRecognizer:recognizer];
}
self.revealViewController.panGestureRecognizer.cancelsTouchesInView = NO;
// NSArray* gestureRecognizers = [self.navigationController.navigationBar gestureRecognizers];
// for (UIGestureRecognizer *gestureRecognizer in gestureRecognizers) {
// if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
// gestureRecognizer.enabled = YES;
//
// break;
// }
// }
//[self.revealViewController.panGestureRecognizer removeTarget:self action:#selector(revealToggle:)];
}
But Pan Gesture is still swiping on Login but when I go to internal screen and comes on Login Screen so it get remove, but why isnt it removing from the first time of application start
My Storyboard Image as below
As I understood, your login screen shouldn't be the front view of SWRevealViewController, cause you don't need a sidebar in it. Create a modal segue from your login view controller to the reveal view controller and perform it when user has logged in successfully. Delete the segue from reveal view controller to login's navigation controller, but create one from reveal to survey choice (don't forget to set it's identifier to sw_front and embed survey choice controller in navigation controller). Create segues from sidebar's logout button to login's navigation view controller and perform it when user wants to logout.
To be more clear, I add photo of my own app, that has similar thing.
http://i.stack.imgur.com/f3nWe.png

detect editButtonItem tap

I have a UITableView with an editButtonItem in the navigation bar. I wanted to have a tap sound play whenever the user taps the editButtonItem. Right now, I'm using the following method to play the tap sound when edit button is tapped
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
//Code to play the tap sound file
[super setEditing:editing animated:animated];}
But the problem I have is that the tap sound also plays when user swipes a tableviewcell & the delete button shows up, which is not something I want. So, my question is, is there a better way to detect when the editButtonItem is tapped?
The below code will play a sound ONLY when the edit button is tapped. When you tap Done it will not play a sound. Also, when you swipe a cell, the sound should not play.
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
if (state == UITableViewCellStateShowingDeleteConfirmationMask) {
swipedToDelete = YES; // BOOL ivar
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
if (editing && !swipedToDelete)
{
// Play sound
}
if (swipedToDelete) {
swipedToDelete = NO;
}
}

iOS:View controller crashes when trying to use camera in an application.

I have a button where when the user presses it, he is navigated to camera to take a pic.
When he takes the pic, he presses done and gets back to the controller. But when that happens the navigation bar gets up in the screen, gets below the battery/signal bar that the telephone has. The weird thing is that this happens on 4/4s but not on 3gs
It is rather tough to answer the question without knowing a bit more details, but here's some code I use to bring up the camera, take the picture, then close the camera successfully. The method can be called by using one line of code: [self takePhoto];
- (void) takePhoto {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//Clear out the UI first
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; -> use this if you want them to select from the library
[self presentViewController:picker animated:YES completion:nil];
} else {
//Device does not support a camera, show a message if desired
}
}
Then you need a delegate for it all so the program knows what to do when an image is taken or selected and how to close, that's what this code is:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.workingImage = nil;
//I grab the image and store globally, but first I manually scale and rotate it if necessary
self.workingImage = [self scaleAndRotateImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
//I display the image in my UI view, so this chunk of code will size it properly
CGRect bound;
bound.origin = CGPointZero;
bound.size = img.size;
imageView.bounds = bound;
//Set the UI image view and dismiss the controller
[imageView setImage:self.workingImage];
[picker dismissModalViewControllerAnimated:YES];
}
Obviously, make sure your controller .h implements the delegate properly like so:
#interface ViewController : UIViewController<UIImagePickerControllerDelegate> { ... }
Hope this helps?

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

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.
add
[picker
dismissModelViewControllerAnimated:YES];
to delegate method bodies.
It will dismiss the view.