Taking Pictures with the Camera IPHONE APPS

UIKit provides access to a device’s camera through the UIImagePickerController class. This class displays the standard system interface for taking pictures using the available camera. It also supports optional controls for resizing and cropping the image after the user takes it. This class can also be used to select photos from the user’s photo library.

The view representing the camera interface is a modal view that is managed by the UI Image Picker Controller class. You should never access this view directly from your code. To display it, you must call the present Modal View Controller: animated: method of the currently active view controller, passing a UI Image Picker Controller object as the new view controller. Upon being installed, the picker controller automatically slides the camera interface into position, where it remains active until the user approves the picture or cancels the operation. At that time, the picker controller notifies its delegate of the user’s choice.

Interfaces managed by the UI Image Picker Controller class may not be available on all devices. Before displaying the camera interface, you should always make sure that the interface is available by calling the is Source Type Available: class method of the UI Image Picker Controller class. You should always respect the return value of this method. If this method returns NO, it means that the current device does not have a camera or that the camera is currently unavailable for some reason. If the method returns YES, you display the camera interface by doing the following:

  1. Create a new UIImagePickerController object.
  2. Assign a delegate object to the picker controller.
  3. In most cases, the current view controller acts as the delegate for the picker, but you can use an entirely different object if you prefer. The delegate object must conform to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocols.

  4. Set the picker type to UIImagePickerControllerSourceTypeCamera.
  5. Optionally, enable or disable the picture editing controls by assigning an appropriate value to the allowsImageEditing property.
  6. Call the presentModalViewController:animated: method of the current view controller to display the picker.

Listing shows the code representing the preceding set of steps. As soon as you call the present Modal View Controller: animated method, the picker controller takes over, displaying the camera interface and responding to all user interactions until the interface is dismissed. To choose an existing photo from the user’s photo library, all you have to do is change the value in the source Type property of the picker to UI Image Picker Controller Source Type Photo Library.

Displaying the interface for taking pictures

When the user taps the appropriate button to dismiss the camera interface, the UI Image Picker Controller notifies the delegate of the user’s choice but does not dismiss the interface. The delegate is responsible for dismissing the picker interface. (Your application is also responsible for releasing the picker when done with it, which you can do in the delegate methods.) It is for this reason that the delegate is actually the view controller object that presented the picker in the first place. Upon receiving the delegate message, the view controller would call its dismiss Modal View Controller Animated: method to dismiss the camera interface.

Listing shows the delegate methods for dismissing the camera interface displayed in
Listing. These methods are implemented by a custom My ViewController class, which is a subclass of UI View Controller and, for this example, is considered to be the same object that displayed the picker in the first place. The use Image: method is an empty place holder for the work you would do in your own version of this class and should be replaced by your own custom code.

Delegate methods for the image picker

If image editing is enabled and the user successfully picks an image, the image parameter of the image Picker Controller: did Finish Picking Image: editingInfo: method contains the edited image. You should treat this image as the selected image, but if you want to store the original image, you can get it (along with the crop rectangle) from the dictionary in the editingInfo parameter.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status