iOS - Camera Management

Digicam is one of the typical functions in a cellular phone. It is possible for us to take images with you and use it in our program and it is quite easy too.

Camera Management – Steps Involved

Step 1. Create a simple View based application.
Step 2. Add a button in ViewController.xib and create IBAction for the button.
Step 3. Add an image view and create IBOutlet naming it as imageView.
Step 4. Update ViewController.h as follows −
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate>
{   
   UIImagePickerController *imagePicker;
   IBOutlet UIImageView *imageView;    
}
- (IBAction)showCamera:(id)sender;

@end

Step 5. Update 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 show camera button, we'll get the following output −
Once we take a picture, we can edit the picture, i.e., move and scale as shown below −

1 comments :

Write comments
21 September 2020 at 22:31 delete

Very nice article,keep sharing more articles with us.

thank you,keep updating....

ios online training

Reply
avatar