Camera:
UIImagePickerController and UIImagePickerControllerDelegate

The app gives you only one chance to pick a photo or video. Don’t press the green camera button in the iPhone simulator. You’ll get the message “photos can only be captured on HW” in the dbg window, and the app will go into an infinite loop.

Source code in Camera.zip

  1. main.m
  2. Class CameraAppDelegate
  3. Class CameraController

The path into the app

CameraController.h shows that the CameraController is three kinds of delegate: UIActionSheetDelegate, UINavigationControllerDelegate, and UIImagePickerControllerDelegate.

  1. The application delegate creates a view controller of class CameraController.

  2. The CameraController creates a plain vanilla view with a yellow background.

  3. The CameraController also creates an action sheet, which Nikita showed us in alert.html. The action sheet contains three buttons, not counting the Cancel button.
    1. UIImagePickerControllerSourceTypeCamera
    2. UIImagePickerControllerSourceTypePhotoLibrary (photo albums)
    3. UIImagePickerControllerSourceTypeSavedPhotosAlbum (camera roll)

  4. When one of the three buttons is pressed, the action sheet’s delegate (which happens to be the CameraController) destroys the action sheet and creates the UIImagePickerController. The UIImagePickerController is also a navigation controller, so we’ll see a navigation bar (at least on top of the photo albums and saved photos).

  5. When the UIImagePickerController has gotten a picture from the camera, photo albums, or camera roll, the UIImagePickerController’s delegate (which happens to be the CameraController) destroys the UIImagePickerController. The delegate also displays the picture in an image view inside the CameraController’s view.

An array in the language C

The actionSheet:clickedButtonAtIndex: method of the CameraController needs an array of three numbers of type UIImagePickerControllerSourceType, which is another name for NSUInteger. But an Objective-C array can only hold objects, not integers. Instead of encasing each number in an NSNumber object (as we did in array3.html), it’s simpler to resort to an array in the language C. The keyword static ensures that the array will be created only the first time the method is called.

Things to try

  1. Do you want a second chance to pick a photo? Create a TouchesBegan:withEvent: method for class CameraController. It will be called whenever the user touches the yellow background. This method could create another action sheet.