Swift UIImagePickerController
Uploading user photos and videos to a server is a common practice for customising a user’s profile or creating a post on social media. For example, Facebook, Twitter and WhatsApp, etc. Apple provides a simple-to-use tool UIImagePickerController
to allow developers to implement this feature within the app in an easy way.
Developers can define the media type (photo or video) and the input source (user’s album or camera) of the file. Users can also crop the selected image and cut the selected video before sending it to the server.
This article will cover the setup procedures and the main properties of each media selection method. Hope this can help you learn more about media selection in iOS! Let’s start!
Select a Photo and Video from Album
Part 1— sourceType
sourceType
represents the origin of the files. It can be either:
photoLibrary
(default) shows album selection pagesavedPhotosAlbum
shows the Moments page directly
Part 2— mediaTypes
mediaTypes
defines the types of file shown at the UIImagePickerViewController
and can be one of the below:
UTType.image.identifier
represents image sourceUTType.video.identifier
represents video source
Part 3 — allowEditing
allowEditing
means the user can edit the photo and video after selecting them. Image can be cropped and video can be cut.
Part 4 — Setting delegate
The delegate
property of the UIImagePickerController
is in a special type. It is both UIImagePickerControllerDelegate
and UINavigationControllerDelegate
. The details will be discussed the later session.
Part 5 — Present UIImagePickerViewController
The final part is just simply presenting the UIImagePickerViewController
as a normal UIViewController
.
Capture photo and video
Part 1: Camera & Microphone Permissions
Camera and microphone permissions must be granted before taking photos or recording a video. The developer must declare the NSCameraUsageDescription
and NSMicrophoneUsageDescription
at the info.plist
and the system will then handle the permission request for us.
Part 2 — mediaTypes
mediaTypes
defines the types of files captured by the camera.
Part 3: Camera settings
There are 3 standard camera parameters:
cameraCaptureMode
: Either.photo
(default) or.video
. It defines whether photo or video is the initial media type when the camera is launched.cameraDevice
: Either.rear
(default) or.front
cameraFlashMode
: Can be.auto
(default),.on
or.off
.
Part 4 — Other: allowEditing, setting delegate & presenting UIImagePickerViewController
Other properties are the same as the one for selecting media from the user’s album. Please refer to the previous session for more information.
UIImagePickerControllerDelegate
UIImagePickerControllerDelegate
is used to handle the user’s actions, including confirmation and cancellation of media selection. There are only two callbacks of UIImagePickerControllerDelegate
:
1. imagePickerController(_:didFinishPickingMediaWithInfo:)
This is the callback when the user has just selected or captured a media (after optionally editing the file).
The info
contains all the data of the selected file and mediaType
can be used to distinguish whether an image or video file is chosen. Different selected files return different information.
Original image and edited image can be retrieved back by the key originalImage
and editedImage
. Edited video URL can be retrieved by mediaURL
.
2. imagePickerControllerDidCancel(_:)
This callback is triggered when the cancel button is pressed in either the media selection mode or capture mode.
In both callbacks of
UIImagePickerControllerDelegate
, it is developer’s responsibility to dismiss theUIImagePickerController
when user confirms or cancel the selection.
Limitation
- Only 1 file can be selected each time
- The duration of captured video cannot be longer than 10 mins by default.
- No grid line can be shown
- Not all photo types can be supported by UIImagePickerViewController. For example, panorama, live photos, time-lapse and slow motion
Summary
UIImagePickerViewController
provides an easy to use API for a user to select either photo or video from device. Media files can be selected or captured from the Moments, album or camera.- Camera and microphone permissions are required to record video and capture photos by the camera. Developers must declare the
NSCameraUsageDescription
andNSMicrophoneUsageDescription
at theinfo.plist
- The camera can be configured with capture mode, camera device and flash mode. However, there is no grid line and extra photo mode in
UIImagePickerViewController
. - Developer needs to dismiss
UIImagePickerViewController
once the user has confirmed or cancelled the media selection process.
Reference
1. Apple UIImagePickerViewController official documentation
You are welcome to follow me at Twitter@myrick_chow for more information and articles. Thank you for reading this article. Have a nice day! 😄