How to Work with Images in iOS Apps

By Rajiv Ramnath

Below we demonstrate how to work with images in iOS apps, at an abstraction level similar to working with video recordings. Start by showing you how variables are initialized in the ViewDidLoad method (specifically, note the initialization of the member variable localImage).

- (void)viewDidLoad{
 [super viewDidLoad];
 // Do any additional setup after loading the view.
 NSString* imagePath =
  [[NSBundle mainBundle] pathForResource:@"Images.bundle/BookFront"
  localImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

Here you see how images are shown:

- (IBAction) showImage:(id)sender{
 imageView.image = localImage;
 [imageView setNeedsDisplay];

Finally, here is the takeImage method that contains code for taking the picture:

- (IBAction) takeImage:(id)sender{
 UIImagePickerController *imagePicker =
  [[UIImagePickerController alloc] init];
 imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
 imagePicker.mediaTypes =
  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
 imagePicker.delegate = self;
 [[UIApplication sharedApplication] setStatusBarHidden:YES];
 [self presentViewController:imagePicker animated:YES completion:nil];

The code for taking a still image is almost identical to that for recording a video. The only difference is that the mediaTypes property of the imagePicker object is set to kUTTypeImage (whereas it’s set to kUTTypeMovie when recording a video).

By now, you’re probably boringly familiar with the use of the view controller as a delegate object to imagePicker and with the purpose of the following two methods: imagePickerControllerDidCancel and didFinishPickingMediaWithInfo. This imagePickerControllerDidCancel is called when the user cancels the operation:

Preview of an app when a user takes a picture.

- (void) imagePickerControllerDidCancel:(UIImagePickerController *) picker {
 [[UIApplication sharedApplication] setStatusBarHidden:NO];
 [picker dismissViewControllerAnimated:YES completion:nil];

As you can see, didFinishPickingMediaWithInfo is called after the picture is taken. Here, the newly taken image is set into the image view of the Image view controller.

Preview of an image after it is taken.

- (void) imagePickerController: (UIImagePickerController *) picker
     didFinishPickingMediaWithInfo: (NSDictionary *) results {
 localImage =
  (UIImage *)[results objectForKey:UIImagePickerControllerOriginalImage];
 imageView.image = localImage;
 [imageView setNeedsDisplay];
 [[UIApplication sharedApplication] setStatusBarHidden:NO];
 [picker dismissViewControllerAnimated:YES completion:nil];

The code for using the video recorder within an app and the code for taking images as well as the code for sending e-mail and SMS are very similar in structure. In both cases, an object of a task-specific view controller is instantiated by the view controller currently running.

In the e-mail case, it’s the MFMailComposeViewController; in the SMS case, it’s the MFMessageComposeViewController; and in the video recording and image capture cases, it’s the UIImagePickerController. The currently running view controller then sets itself as a delegate within the task-specific view controller. Finally, the task-specific view controller is presented, and the current view controller exits.

When the task-specific view controller (that is, the instance of the UIImagePickerController, the MFMailComposeViewController, or the MFMessageComposeViewController) exits, the delegate method of the original view controller is called, either to process the data returned by the recorder or the camera or to deal with errors and cancellations.