-3

how show directly camera in main view controller with hidden all default buttons and take one our custom button, when i click the button i need photo in UIImageview ?

kb920
  • 3,039
  • 2
  • 33
  • 44
SWAMY CHUNCHU
  • 225
  • 5
  • 14
  • [have look about app life cycle process first](http://stackoverflow.com/a/14644625/4003548) then apply camera code. – vaibhav Oct 14 '16 at 08:11
  • that is an old document, you might have wanted to refer this: https://developer.apple.com/reference/uikit/uiviewcontroller – holex Oct 14 '16 at 16:16

6 Answers6

1

To hide all default buttons you need to set showsCameraControls= NO property of UIImagePickerController.

And add your custom button on UIImagePickerController

UIImagePickerController *imgPicker;

-(void)openCustomCamera{
    imgPicker = [[UIImagePickerController alloc] init];
    [imgPicker setDelegate:self];
    [imgPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    imgPicker.showsCameraControls = NO;

     //Custom button
     UIButton *customBtn = [[UIButton alloc] initWithFrame:CGRectZero];
     [customBtn addTarget:self action:@selector(customButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    //Add your custom button to imagepickerview
    [imgPicker.view addSubView:customBtn];
    // Show image picker
    [self presentViewController:imagePicker animated:YES completion:^{}];

}

Now in your custom button action method add code to take photo

-(void)customButtonAction:(id)sender{
   [imgPicker takePicture];
}

Add your delegate method to get image data in delegate method

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
        //Access the uncropped image from info dictionary
        [picker dismissViewControllerAnimated:YES completion:^{
            UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
            if(image)
        }];
}
Ashish Shah
  • 2,361
  • 1
  • 20
  • 20
1
Add this code in app delegate and call openCamera in applicationDidBecomeActive

-(void)openCamera
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = YES;
        [self.window.rootViewController presentViewController:imagePicker animated:true completion:nil];


    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Camera not available"
                                                       message:@"No Camera available on your device."
                                                      delegate:nil
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil, nil];
        [alert show];
        alert = nil;
    }
}
Add Delegates    

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.window.rootViewController dismissViewControllerAnimated:true completion:nil];

}
User511
  • 1,456
  • 10
  • 28
  • _"Add this code in app delegate and call openCamera in applicationDidBecomeActive"_. how about __NO__. – holex Oct 14 '16 at 16:13
0

try it for swift 3....

override func viewDidAppear(animated: Bool) 
{

    if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) 
    {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        presentViewController(imagePicker, animated: true, completion: nil)
    }
}
seggy
  • 1,176
  • 2
  • 18
  • 38
0

Add UIImagePickerControllerDelegate delegate in your .h file and implement in didfinish launch. Don't forget to implement the delegate.

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {


            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = NO;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:NULL];

        }
        else
        {
            UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                  message:@"Device has no camera!"
                                                                 delegate:nil
                                                        cancelButtonTitle:@"OK"
                                                        otherButtonTitles: nil];
            [myAlertView show];
        }

Delegate :

#pragma mark - UIImagePickerController Deleagte

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
 {

 [picker dismissViewControllerAnimated:YES completion:NULL];


UIImage *image =info[UIImagePickerControllerOriginalImage];  

 // NOW set the image on you imageview like
  self.myImageview.image=image;

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

  [picker dismissViewControllerAnimated:YES completion:NULL];

}
Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49
0

Do as follow, Hope it will work for you.

1) Implement UIImagePickerControllerDelegate

2) Call when your Application became active

 - (void)applicationDidBecomeActive:(UIApplication *)application {
        NSLog(@"appdelegate applicationDidBecomeActive");
        [self actionLaunchAppCamera];
    }

3) Method for Open camera

-(void)actionLaunchAppCamera
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = YES;
        [self.window.rootViewController presentModalViewController:imagePicker animated:YES];
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Camera Unavailable"
                                                       message:@"Unable to find a camera on your device."
                                                      delegate:nil
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil, nil];
        [alert show];
        alert = nil;
    }
}

4) define delegate method as follow.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.window.rootViewController dismissModalViewControllerAnimated:YES];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self.window.rootViewController dismissModalViewControllerAnimated:YES];
}
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
0

This is the swift 3 code for capture image from camera when you click captureCamera button.

@IBAction func captureCamera(sender: UIBarButtonItem) {
        if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
            picker.allowsEditing = false
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            picker.cameraCaptureMode = .Photo
            presentViewController(picker, animated: true, completion: nil)
        } else {
            noCamera()
        }
    }
 func imagePickerController(
        picker: UIImagePickerController,
        didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
        myImageView.image = chosenImage //4
        dismissViewControllerAnimated(true, completion: nil) //5    
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }

Don't forget to add this picker.delegate = self to your viewDidLoad() function.

G A
  • 67
  • 2
  • 13