UIImagePickerController
extension UIImagePickerController
The following is an example of using closures to
select media from a UIImagePickerController
.
A simple picker is created which defaults to allowing
images to be selected from the photos library. This
initializer will call the handler when an image is selected.
When preseting with the present(from:)
method,
the UIImagePickerController
will dismiss itself on user cancel
or after the picker has picked its content (after the closure
is called).
UIImagePickerController() { result,picker in
myImageView.image = result.editedImage
}.present(from: self)
You can also customize the picker if you need more control, including setting your own initial values and delegate callbacks. The following is a verbose example using most of the possible customization points.
UIImagePickerController(
source: .camera,
allow: [.image, .movie],
cameraOverlay: nil,
showsCameraControls: false) { result,picker in
myImageView.image = result.originalImage
}.didCancel { [weak self] in
// some custom didCancel implementation
self?.dismiss(animated: animation.animate)
}.present(from: self)
-
This is a convenience initializer that allows you to setup your UIImagePickerController with some commonly used default settings. In addition, it provides a default handler for a cancelled picker, but you are free to provide your own
didCancel
implementation using thedidCancel
parameter. The only required parameter is thedidPick
closure, which will be called when the user picks a medium.Important
The picker will not be attempted to be dismissed after this closure is called, unless you use thepresent(from:animation:)
method.
An example of initializing with this method:
let picker = UIImagePickerController( source: .camera, allow: [.image, .movie]) { [weak self] result,picker in myImageView.image = result.originalImage self?.dismiss(animated: animation.animate) } }
Declaration
Swift
public convenience init(source: UIImagePickerController.SourceType = .photoLibrary, allow: UIImagePickerController.MediaFilter = .image, cameraOverlay: UIView? = nil, showsCameraControls: Bool = true, didCancel: @escaping ( _ picker: UIImagePickerController) -> Void = dismissFromPresenting, didPick: @escaping (_ result: UIImagePickerController.Result, _ picker: UIImagePickerController) -> Void)
Parameters
source
The
UIImagePickerControllerSourceType
that UIImagePickerController expects to determine where the content should be obtained from. This value will be applied to the picker’ssourceType
property.allow
A convenience
OptionSet
filter based onkUTType
Strings. These types will be applied to the picker’s mediaTypes property.cameraOverlay
This is equivalent to
UIImagePickerController
‘scameraOverlayView
property and will be applied as such.showsCameraControls
This is equivalent to
UIImagePickerController
‘sshowsCameraControls
property and will be applied as such.didCancel
The closure that is called when the
didCancel
delegate method is called. The default is to attempt to dismiss the picker.didPick
The closure that is called when the imagePickerController(_:didFinishPickingMediaWithInfo:) is called. The closure conveniently wraps the Dictionary obtained from this delegate call in a
Result
struct. The original Dictionary can be found in therawInfo
property. -
A convenience method that will present the UIImagePickerController. It will also do the following as default behavior, which can be overriden at anytime:
- Default the didCancel callback to dismiss the picker.
- Call the didFinishPickingMedia handler if one was set previously, usually
from the convenience initiaizer’s
didPick
parameter or by calling thedidFinishPickingMedia(_:)
method. - Dismiss the picker after calling the
didFinishPickingMedia
handler.
An example of calling this method using the convenience initializer:
let picker = UIImagePickerController( source: .photoLibrary, allow: .image) { result,_ in myImageView.image = result.originalImage }.present(from: self)
Declaration
Swift
@discardableResult public func present(from viewController: UIViewController, animation: (animate: Bool, onComplete: (() -> Void)?) = (true, nil)) -> Self
Parameters
viewController
The view controller that is able to present the picker. This view controller is also used to dismiss the picker for the default dismissing behavior.
animation
An animation info tuple that describes whether to animate the presenting and what to do after the animation is complete.
Return Value
itself so you can daisy chain the other delegate calls
-
Equivalent to implementing UIImagePickerControllerDelegate’s imagePickerController(_:didFinishPickingMediaWithInfo:) method
Declaration
Swift
@discardableResult public func didFinishPickingMedia(handler: @escaping (_ withInfo: [UIImagePickerController.InfoKey: Any]) -> Void) -> Self
Parameters
handler
The closure that will be called in place of its equivalent delegate method
Return Value
itself so you can daisy chain the other delegate calls
-
Equivalent to implementing UIImagePickerControllerDelegate’s imagePickerControllerDidCancel(_:) method
Declaration
Swift
@discardableResult public func didCancel(handler: @escaping () -> Void) -> Self
Parameters
handler
The closure that will be called in place of its equivalent delegate method
Return Value
itself so you can daisy chain the other delegate calls
-
A wrapper around
kUTType
. Eventually these will be replaced by Apple with a Swifty enum, but until then, this simply wraps these values in a strongly typedOptionSet
. You can use these when deciding which media types you want the user to select from theUIImagePickerController
using theinit(source:allow:cameraOverlay:showsCameraControls:didPick:)
initializer.Since it is an
OptionSet
, you can use array literal syntax to describe more than one media type.
See morelet picker = UIImagePickerController(allow: [.image, .movie]) {_,_ in // ... }
Declaration
Swift
public struct MediaFilter : OptionSet
-
This result object is a only a wrapper around the loosely typed Dictionary returned from
See moreUIImagePickerController
‘s-imagePickerController:didFinishPickingMediaWithInfo:
delegate method. When using theinit(source:allow:cameraOverlay:showsCameraControls:didPick:)
initializer, thedidPick
closure passes this convenience struct. If the original Dictionary is needed, it can be found in therawInfo
property.Declaration
Swift
public struct Result
-
Clears any delegate closures that were assigned to this
UIImagePickerController
. This cleans up memory as well as sets thedelegate
property to nil. This is typically only used for explicit cleanup. You are not required to call this method.Declaration
Swift
@objc public func clearClosureDelegates()