Here’s some code that takes a picture and then displays the result on your phone.

Note: To run this example you’ll need an apple developer certificate and your iPhone hooked up to XCode.

Taking the picture

There’s some extra checks going on here (like seeing if the device has a camera and then iterating the media types to see if the right one is there).

Importing MobileCoreServices

if ([mediaTypes containsObject:(NSString *)kUTTypeImage]) 

kUTTypeImage is an object type defined in MobileCoreServices. Before this example is run you will need to import that library into your project (here’s a reminder on how to do that).

Implementing the delegates

This interface implements x2 delegates:

UIImagePickerControllerDelegate is obvious. UINavigationControllerDelegate is not. Due to some overloading of method names you have to implement the UINavigationControllerDelegate even though it’s not required.

The imagePickerController method does a nice job of shrinking the size of the image until it’s something that will fit the frame.

The results look ugly (an image just slapped on the view) but when you run it and press the ‘Take Picture’ button, your phone should switch to camera and then display whatever picture you take on screen.

Here’s the code in it’s entirety.

ViewController.m

#import "Spike2ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>

@interface Spike2ViewController() <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *button;
@end

@implementation Spike2ViewController
@synthesize button = _button;


- (IBAction)takePicture:(id)sender 
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        if ([mediaTypes containsObject:(NSString *)kUTTypeImage]) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
            picker.allowsEditing = YES;
            [self presentModalViewController:picker animated:YES];
        }
    }
}

- (void)dismissImagePicker
{
    [self dismissModalViewControllerAnimated:YES];
}

#define MAX_IMAGE_WIDTH 200

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (!image) image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (image) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        CGRect frame = imageView.frame;
        while (frame.size.width > MAX_IMAGE_WIDTH) {
            frame.size.width /= 2;
            frame.size.height /= 2;
        }
        imageView.frame = frame;
        [self.view addSubview:imageView];
    }
    [self dismissImagePicker];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissImagePicker];
}


- (void)viewDidUnload {
    [self setButton:nil];
    [self setView:nil];
    [super viewDidUnload];
}

@end

For a full walkthrough of this on an iPad check out Lecture 16 of Paul Hegarty’s iPhone App Dev course.