Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

iOS camera management


May 21, 2021 iOS Development Manual


Table of contents


IOS camera management


Introduction to the camera

Cameras are one of the common features of mobile devices, we can use the camera to take pictures and call it in the app, and the camera is very simple to use.

The instance step

1, create a simple View base application

2. Add a button to ViewController.xib and create an IBAction for the button

3, add an image view (image view) and create an IBOutlet called imageView

4, ViewController.h file code is as follows:

#import <UIKit/UIKit.h>

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

@end

5, modify the 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 get the output below

iOS camera management

Once you're taking a picture, you can edit the picture by moving and zooming, as shown below.

iOS camera management