Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

iOS Camera Management

IOS Camera Management


Introduction to the Camera

The camera is one of the common features of mobile devices. We can use the camera to take pictures and call it in our application, and the use of the camera is very simple.

Example Steps

1. Create a simple View based application

2. Add a button in ViewController.xib and create an IBAction for that button

3. Add an image view and create an IBOutlet named imageView

4. The ViewController.h file code is as follows:



#import 



@interface ViewController : UIViewController

{   

   UIImagePickerController *imagePicker;

   IBOutlet UIImageView *imageView;    

}

- (IBAction)showCamera:(id)sender;



@end

5. Modify ViewController.m as follows:



#import "ViewController.h"



@interface ViewController ()



@end



@implementation ViewController



- (void)viewDidLoad

{

    [super viewDidLoad];    

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



- (IBAction)showCamera:(id)sender {

    imagePicker.allowsEditing = YES;

    if ([UIImagePickerController isSourceTypeAvailable:

        UIImagePickerControllerSourceTypeCamera])

    {

        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    }

    else{

        imagePicker.sourceType = 

        UIImagePickerControllerSourceTypePhotoLibrary;

    }

    [self presentModalViewController:imagePicker animated:YES];



}

-(void)imagePickerController:(UIImagePickerController *)picker 

  didFinishPickingMediaWithInfo:(NSDictionary *)info{

    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

    if (image == nil) {    

        image = [info objectForKey:UIImagePickerControllerOriginalImage];

    }

    imageView.image = image;

    

}



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

    [self dismissModalViewControllerAnimated:YES];

}



@end

Output

When we run the application and click the Show Camera button, we will get the following output

camera_Output1

Once the picture is taken, the image can be edited by moving and scaling, as shown below.

camera_Output2