iOS Start Camera

One of the hot topic in iOS is camera, isn’t it?
There are a lot of camera application in App Store.
Sometimes we need filter or special customize for it.
But there are also cases just taking picture and saving anywhere.

This is the first step of camera programming.

UIImagePickerController

UIImagePickerController is prepared class to handle image picker, camera roll.

UIImagePickerControllerDelegate

This is handler protocol to manage UIImagePickerController operation

Delegate method Description
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info Handler to retrieve image from picker
-(void)imagePickerController:(UIImagePickerController *)picker Just finish

Source Type

Sample

CameraTestViewController.h

@interface CameraTestViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@end

CameraTestViewController.m

@interface CameraTestViewController ()

@property (nonatomic) UIImageView *imageView;

@property (nonatomic) UIButton *takePhotoButton;
@property (nonatomic) UIButton *selectPhotoButton;

@end

@implementation CameraTestViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    CGRect imgRect = CGRectMake(self.view.frame.origin.x,
                             self.view.frame.origin.y,
                             self.view.frame.size.width,
                             self.view.frame.size.height - 30);
    
    CGRect takePhotoRect = CGRectMake(self.view.frame.origin.x,
                                self.view.frame.origin.y + self.view.frame.size.height - 28,
                                self.view.frame.size.width / 3,
                                25);
    
    CGRect selectPhotoRect = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width * 2 / 3,
                                      self.view.frame.origin.y + self.view.frame.size.height - 28,
                                      self.view.frame.size.width / 3,
                                      25);
    
    
    self.imageView = [[UIImageView alloc] initWithFrame:imgRect];
    
    self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.takePhotoButton.frame = takePhotoRect;
    [self.takePhotoButton setTitle:@"Take Photo" forState:UIControlStateNormal];
    
    self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.selectPhotoButton.frame = selectPhotoRect;
    [self.selectPhotoButton setTitle:@"Select Photo" forState:UIControlStateNormal];
    
    [self.takePhotoButton addTarget:self action:@selector(takePhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.selectPhotoButton addTarget:self action:@selector(selectPhoto:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.imageView];
    [self.view addSubview:self.takePhotoButton];
    [self.view addSubview:self.selectPhotoButton];
}

#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *selectedImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = selectedImage;
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

-(void)imagePickerController:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - Event
-(void)takePhoto:(id)sender {
    
    if ( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"Device has no camera"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles: nil];
        
        [alertView show];
        
    }
    else {
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:NULL];
    }
}

-(void)selectPhoto:(id)sender {
    
    if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
    
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //UIImagePickerControllerSourceTypeSavedPhotosAlbum
        [self presentViewController:picker animated:YES completion:NULL];
    }
}
@end

Result

Camera UI
If push “Select Photo”
Photo select

Ref

Build a Simple Camera App Using UIImagePickerController