Accessing photos in iOS swift 3

3 min readMar 18, 2017

Let’s see how we can access the camera and photo library in iOS. For this purpose we use the UIImagePickerController class. It’s a view controller that returns an UIImage . It has its own UI for camera with basic controls and a UI for the photo library.

First let us see how to capture a photo using UIImagePickerController.For that , first we have to create an instance of UIImagePickerController class.

let imagePicker = UIImagePickerController()

Then we have to set the UIImagePickerController source type. It’s actually an enum with 3 cases. Default value is UIImagePickerControllerSourceTypePhotoLibrary. The role and appearance of an image picker controller depend on the source type you assign to it before you present it.

  • A sourceType of camera provides a user interface for taking a new picture or movie (on devices that support media capture).
  • A sourceType of photoLibrary or savedPhotosAlbum provides a user interface for choosing among saved pictures and movies.
public enum UIImagePickerControllerSourceType : Int {case photoLibrarycase cameracase savedPhotosAlbum}

Now, we have to set the mediatypes array for UIImagePickerController.

When capturing media, the value of this property determines the camera interface to display. When browsing saved media, this property determines the types of media presented in the interface.

By default, this property is set to the single value kUTTypeImage, which designates the still camera interface when capturing media, and specifies that only still images should be displayed in the media picker when browsing saved media. To designate the movie capture interface, or to indicate that only movies should be displayed when browsing saved media, use the kUTTypeMovie identifier in a statement like this:

imagePicker.mediaTypes = [kUTTypeMovie as String] // the default value is kUTTypeImage ..

To designate all available media types for a source, use a statement like this:

imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(!

Note : You should import MobileCoreServices to use mediatypes.

Also, we have to confirm to the UIImagePickerControllerDelegate along with UINavigationControllerDelegate .

Getting the image form the info dictionary

UIImagePickerController give a dictionary with information about the selected media. You can find the UIImageinstance representing the selected image on the UIImagePickerControllerOriginalImage key. The ifo dictionary may contain:

Since iOS simulator does not have a camera, It’s a good idea to check if the camera is available, you can use the UIImagePickerController.isSourceTypeAvailalbe(_:)class method for this.

source code

Asking for permission

The app must ask for permission from the user before accessing the camera/ the saved photos.The app should display a message to the user explaining why it needs the camera or photo library access. You can set this message by setting the NSCameraUsageDescription and NSPhotoLibraryUsageDescription key in the Info.plist file of your app.

plist file

If this is not set, the app will crash giving a message like this :Error: [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.

If it is set, then the app will prompt the user to give permission to access the camera / saved photos.

permission prompt

Custom overlays for UIImagePickerController

For adding a custom overlay UI for your UIImagePickerController, set theUIImagePickerController's showCameraControls property to NO. Then you should set the cameraOverlayView property of UIImagePickerController.

Apple has a good tutorial called PhotoPicker which shows how to customize cameraOverlayView.

Here is the link to the github source code.

If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!