How to open Camera directly after did finish launching?

1.3k Views Asked by At

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 ?

6

There are 6 best solutions below

0
seggy On

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)
    }
}
0
Jamshed Alam On

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];

}
0
Hitesh Surani On

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];
}
0
Ashish Shah On

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)
        }];
}
1
User511 On
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];

}
0
G A On

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.