UIImagePickerController takePicture is not calling any delegate method - iphone

I am creating a sample app which allow use to capture image and record video with custom buttons.
I created an instance of UIImagePicker controller and assign delegate and all is done but takePicture, is not calling delegate method.
I refer few of post too UIImagePickerController takePicture not responding and UIImagepickerController [takepicture] modal view disappears no delegate callback but not getting my solution.
Here is my code. Please let me know if I am missing anything:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.hidden = YES;
imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setAllowsEditing:NO];
[imagePicker setMediaTypes:[NSArray arrayWithObject:#"public.movie"]];
[imagePicker setCameraCaptureMode:UIImagePickerControllerCameraCaptureModeVideo];
[imagePicker setShowsCameraControls:NO];
[imagePicker setToolbarHidden:YES];
[imagePicker setDelegate:self];
UIView *overlayView = [[UIView alloc] initWithFrame:[[imagePicker view]frame]];
[overlayView setBackgroundColor:[UIColor clearColor]];
UIButton *btnCapture = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnCapture setFrame:CGRectMake(0, overlayView.frame.origin.y + overlayView.frame.size.height - 35, 30, 30)];
[btnCapture setTitle:#"c" forState:UIControlStateNormal];
[btnCapture addTarget:self action:#selector(captureFrame:) forControlEvents:UIControlEventTouchUpInside];
UIButton *btnStopVideo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnStopVideo setFrame:CGRectMake(40, overlayView.frame.origin.y + overlayView.frame.size.height - 35, 30, 30)];
[btnStopVideo setTitle:#"s" forState:UIControlStateNormal];
[btnStopVideo addTarget:self action:#selector(btnStopTapped:) forControlEvents:UIControlEventTouchUpInside];
[overlayView addSubview:btnStopVideo];
[overlayView addSubview:btnCapture];
[imagePicker setCameraOverlayView:overlayView];
[self presentModalViewController:imagePicker animated:NO];
}
- (void)captureFrame:(id)sender {
[imagePicker takePicture];
}
But delegate method:- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info is not being called.
Please help me to solve this issue.
Thanks to all

Just add this line
picker.showsCameraControls = NO;

You need to make sure you implement the UIImagePickerControllerDelegate protocol in your header.
.h
#interface MyViewController <UIImagePickerControllerDelegate>
{
//
}
#end

You better to check if your UIImagePickerController.delegate still your controller.

Related

How can I programmatically segue with tapping on an Image?

I've implemented 2 ImageViews with images and tapGestures, this works fine, now I want to segue to another ViewController dependent on which image I've tapped. I don't know how I have to declare my ImageView which ViewController it should call.
so my Code is:
-(void) initImageViews{
self.navigationItem.title = #"Menu";
UIImage *serviceImage = [UIImage imageNamed:#"Service.png"];
self.serviceImageView = [[UIImageView alloc] initWithImage:serviceImage];
[self.serviceImageView setFrame:CGRectMake(20, 20, 48,48 )];
[self.view addSubview:self.serviceImageView];
[self.view setBackgroundColor:[UIColor blackColor]];
UIImage *industryImage = [UIImage imageNamed:#"Industry.png"];
self.industryImageView = [[UIImageView alloc] initWithImage:industryImage];
[self.industryImageView setFrame:CGRectMake(73,20,48,48)];
[self.view addSubview:industryImageView];
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[self.industryImageView setUserInteractionEnabled:TRUE];
[self.serviceImageView setUserInteractionEnabled:TRUE];
[self.serviceImageView addGestureRecognizer:singleTap];
[self.industryImageView addGestureRecognizer:singleTap];
}
- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer {
NSLog(#"Tapped!");
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Service"]) {
SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}if ([segue.identifier isEqualToString:#"Industry"]) {
SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}
}
}
Thanks in advance :)
[self performSegueWithIdentifier:IdentifierForTheSegueiFromStoryBoard sender:nil];
EDIT: the seque must exist in the storyboard and must have an identifier.
If you don't want/need to use segues you could use:
if (i want controller A) {
[self presentViewController:ControllerA animated:YES completion:nil];
} else if (i want controller B)
[self presentViewController:ControllerB animated:YES completion:nil];
}
Create a seague in the storyboard with the identifier.then programatically call a segue like this
[self performSegueWithIdentifier: #"myidentifier" sender: self];

Setting Back Button to a custom UIImage

Currently I'm using my own custom images in UIBarButtonItems with the following code:
UIButton *profileBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35.0f, 35.0f)];
[profileBarButton setImage:[UIImage imageNamed:#"profile-barbutton.png"] forState:UIControlStateNormal];
[profileBarButton addTarget:self.navigationController action:#selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:profileBarButton];
This works perfectly when I have a defined action to call, such as presenting a modalviewcontroller and turning on edit mode. However, I'm confused as to how I'd set the action for going back from one view to another, not modally? Is there a specific method I could call programmatically? Usually the navigationcontroller takes care of this...
for going back from one view to another, not modally you may write like this:
- (void) toggleMenu
{
if (self.navigationController.visibleViewController == self)
{
[self.navigationController popViewControllerAnimated: YES];
}
}
Use this code:
[self dismissViewControllerAnimated:YES completion:nil];
Check with this code...
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyNib" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentModalViewController:nc animated:YES];
[vc release];
[nc release];
Then normally push code like...
OtherViewController *vc = [[OtherViewController alloc] initWithNibName:#"MyOtherNib" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
:)
Try this code:
UIImage *backButtonImage = [UIImage imageNamed:#"backButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBackBarItem;
In the #selector(back), "back" is the method that triggers the popup method of navigationcontroller. Like this:
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}

How do I call a method from a UIbutton selector?

How do I call this method from the button selector code I have below:
- (void)displayEditorForImage:(UIImage *)imageToEdit
{
AFPhotoEditorController *editorController = [[AFPhotoEditorController alloc] initWithImage:imageToEdit];
[editorController setDelegate:self];
[self presentViewController:editorController animated:YES completion:nil];
}
here's the UIButton I'm trying to call the method with:
//edit button
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[editButton addTarget:self
action:#selector(editBtnTouch)
forControlEvents:UIControlEventTouchDown];
[editButton setTitle:#"Effects" forState:UIControlStateNormal];
**// the following line shows the selector where I'm unsure how to call the method from the code above**
if ([[UIScreen mainScreen] respondsToSelector:#selector(displayEditorForImage:)] &&
([UIScreen mainScreen].scale == 2.0)) {
// Retina display
editButton.frame = CGRectMake(220.0, 320.0, 60.0, 40.0);
} else {
editButton.frame = CGRectMake(220.0, 315.0, 60.0, 40.0);
}
[self.view addSubview:editButton];
thanks for the help
When the selector you add to the button is called, the button itself will be passed as the first argument if the selector allows for one. In order to get a reference to your image, you would either need to do this (assuming the image you want is the background image for the button):
- (void)displayEditorForImage:(UIButton*)sender {
UIImage* imageToEdit = [sender backgroundImageForState:UIControlStateNormal];
AFPhotoEditorController *editorController = [[AFPhotoEditorController alloc] initWithImage:imageToEdit];
[editorController setDelegate:self];
[self presentViewController:editorController animated:YES completion:nil];
}
Or you would need to make a custom UIButton that has an extra property for the associated image, and then just access the image through the button in the selector:
- (void)displayEditorForImage:(UIButton*)sender {
if([sender isKindOfClass:[YourCustomButtonClass class]]) {
UIImage* imageToEdit = ((YourCustomButtonClass*)sender).customImageProperty;
AFPhotoEditorController *editorController = [[AFPhotoEditorController alloc] initWithImage:imageToEdit];
[editorController setDelegate:self];
[self presentViewController:editorController animated:YES completion:nil];
}
}
Edit:
You seem to be confused as to how to make a UIButton call a method on when it is tapped. You need to add the object which defines the method as the button's target (in this case self), add the method as the selector, and to called the method when the button is tapped use the UIControlEventTouchUpInside. So your code would be:
[editButton addTarget:self action:#selector(displayEditorForImage:) forControlEvents:UIControlEventTouchUpInside];

Hide record button in UIImagePickerController

I have a question on UIImagePickerController. May I use UIImagePickerController as a preview? If I do like that, I have to hide record button, is there any way to do it? or Any other approach to show our preview other than UIImagePickerController.
thanks for your helps...
You can hide the controls of a UIImagePickerController by using:
[myImagePickerController setShowsCameraControls:NO];
EDIT: Check out this code, it hides all the controls inside the image picker allowing you to create and add your own custom controls. Unfortunately you can not selectively hide controls within the picker so this is the alternative.
- (void)showCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
myImagePicker = [[UIImagePickerController alloc] init];
[myImagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[myImagePicker setDelegate:self];
[myImagePicker setShowsCameraControls:NO];
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UIButton *switchCameraButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[switchCameraButton setFrame:CGRectMake(10, 430, 72, 40)];
[switchCameraButton addTarget:self action:#selector(changeCamera:) forControlEvents:UIControlEventTouchUpInside];
[overlayView addSubview:switchCameraButton];
[myImagePicker setCameraOverlayView:overlayView];
[self presentViewController:myImagePicker animated:YES completion:nil];
}
}
- (IBAction)changeCamera:(id)sender
{
if (myImagePicker.cameraDevice == UIImagePickerControllerCameraDeviceFront) {
[myImagePicker setCameraDevice:UIImagePickerControllerCameraDeviceRear];
}else{
[myImagePicker setCameraDevice:UIImagePickerControllerCameraDeviceFront];
}
}

dismissModalViewController not working as expected

I have a problem that I'm helping someone can shed some light on. I'm sure it is something very simple, but I just can't see it and could really use an extra set of eyes to help find this bug.
The app I'm working on uses UIImagePickerController to present a camera preview with the stock camera UI hidden. This preview implements a cameraOverlay view with a few buttons to perform my required tasks, one of which is to dismiss the camera preview.
For some reason, the function to dismiss the camera view will not work when being called by the button in the overlay view. I can NSLog the function and see that it is in fact being called by the button, but simply doesn't allow the dismissModalViewController to work. I have tested the function by having an NSTimer call it a few seconds after it loads, and it works fine, just not when I call it from the button in the cameraOverlay.
My viewController (CameraTestViewController.m) calls the UIImagePicker as shown below with the press of a button.
-(IBAction) getPhoto:(id) sender {
OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picker.cameraOverlayView = overlay;
picker.showsCameraControls = NO;
picker.toolbarHidden = YES;
[self presentModalViewController:picker animated:NO];
//[self performSelector:#selector(doCancel) withObject:nil afterDelay:10];
}
I dismiss the camera view with this method elsewhere in the viewController
-(void)doCancel
{
NSLog (#"doCancel method has been called");
[picker dismissModalViewControllerAnimated:YES];
}
Then in my OverlayView.m class, I set up the required buttons and provide a method that calls the dismissing of the modal view controller.
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Clear the background of the overlay:
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
...
myButton04 = [UIButton buttonWithType:UIButtonTypeCustom];
myButton04.frame = CGRectMake(10, 447, 44, 13);
[myButton04 setTitle:#"" forState:UIControlStateNormal];
[myButton04 addTarget:self action:#selector(scanButton05TouchUpInside) forControlEvents:UIControlEventTouchUpInside];
[myButton04 setImage:[UIImage imageNamed:#"camControlsBack.png"] forState:UIControlStateNormal];
[self addSubview:myButton04];
...
}
return self;
}
- (void) scanButton05TouchUpInside {
NSLog(#"BACK BUTTON HAS BEEN PRESSED");
CameraTestViewController *baseViewController = [[CameraTestViewController alloc] init];
[baseViewController doCancel];
}
ANY help on this would be most appreciated. I'm sick of having a purple forehead from beating my head against the wall over this one. :cool:
THANKS!
Use this Below code:
[[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES];
The way I dismiss my modalViewController is to call its parent:
[[picker parentViewController] dismissModalViewControllerAnimated:YES];