iOS Camera & Image

Capture Image from an iOS Camera

Declare a View Controller to support the following Delegate

  • UIImagePickerControllerDelegate for the Image Picker
  • UINavigationControllerDelegate for the modal view UI of the Image Picker
    MyViewController.h
    @interface MyViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
    

Implement code to trigger iOS Image Picker for image capturing

MyViewController.m
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
picker.allowsEditing = YES;
[self presentModalViewController:picker animated:YES];
  • Initialize UIImagePickerController
    • Manages either built-in user interfaces for taking pictures and movies
    • Or UI to choose saved images and movies
  • Check if the device has a camera
    • isSourceTypeAvailable options
      UIImagePickerControllerSourceTypePhotoLibrary
      UIImagePickerControllerSourceTypeCamera
      UIImagePickerControllerSourceTypeSavedPhotosAlbum
      
  • If camera is available, set UIImagePickerController to use the picture/movie taking UI (UIImagePickerControllerSourceTypeCamera)
    • UIImagePickerController can be configured to pick picture from a library also
  • Allow the UI to edit the picture
  • presentModalViewController:picker pop up a modal view with this image picker

Image Picker Delegate

When an image is picked by the image picker

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
  if (!image) image = [info objectForKey:UIImagePickerControllerOriginalImage];
  if (!image) return;

  ...

  ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
  [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:metadata completionBlock:nil];

  [self dismissModalViewControllerAnimated:YES];
}
  • create a UIImage
  • save it to an Assest library
  • Dismiss the Image Picker modal view
  • info contains
    UIImagePickerControllerMediaType
    UIImagePickerControllerOriginalImage
    UIImagePickerControllerEditedImage
    UIImagePickerControllerCropRect
    UIImagePickerControllerMediaMetadata
    

Image Picker Properties & Methods

Control video capturing quality

@property UIIPCQualityType qualityType;

Control capturing duration

@property NSTimeInterval videoMaximumDuration;

Show UI button control in the Image Picker UI

@property BOOL showsCameraControls;

Apply a transformation functions when capture image

@property CGAffineTransform cameraViewTransform

Check iOS Media Capability

Check whether a iOS device support image, audio & video

+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType;
  • Return an array of what the device can do with media
    • kUTTypeImage supports image
    • kUTTypeMovie supports audio & movie

Select media type (image/video) after checking device capabilities

NSString*desired=kUTTypeMovie;
if ([[UIIPC availableMediaTypesForSourceType:picker.sourceType] containsObject:desired]) {
    picker.mediaTypes = [NSArray arrayWithObject:desired];
}

Check whether the device has front/back camera

+ (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice;
  • UIImagePickerControllerCameraDeviceFront or
  • UIImagePickerControllerCameraDeviceRear

Check if iOS device support Flash

+ (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice);

Photo/Video Capture Mode

+ (NSArray *)availableCaptureModesForCameraDevice:(UIImagePickerControllerCameraDevice);