Media Picker is an ios reusable component written in Swift 4.2.
This is an attempt to make an easy photo / video picking library from camera / gallery with image crop feature.
-
Picker Mode:
enum PickerMode { case all case camera case library }When user chooses picker mode `.all` , the media picker library checks if both camera and photo library is available or not. If both is available, action sheet is shown to choose from. If one of the picker type is not available, it directly opens the other available picker type
-
Media Type:
enum MediaType: Int { case all case video case photo }When user chooses media type mode `.all` , both video and image selection is enabled from gallery or video and image capture is enabled from camera.
-
Cropping Enabled?
One can enable/disable cropping of the photo selected/clicked. If cropping is enabled, here are a few options one can choose from for cropping featurepublic var aspectRatioPreset: TOCropViewControllerAspectRatioPreset = .preset4x3 public var aspectRatioLockEnabled: Bool = true public var resetAspectRatioEnabled: Bool = true public var rotateButtonsHidden: Bool = true public var croppingStyle: TOCropViewCroppingStyle = .default`
croping style are
.default
and.circular
-
Maximum Photo/video size: There is no restriction in the size of photo or video selected, but if user wants to restrict, there are two public variables available to do so. public var maximumVideoSizeInMb: Int? public var maximumImageSizeInMb: Int?
- Copy and paste the "Media Picker" folder i.e the library folder in your project.
-
Add the pod 'CropViewController' in your pod file and install the pod. https://github.com/TimOliver/TOCropViewController 'CropViewController' is the swift version of 'TOCropViewController'. We use 'CropViewController' as our cropping library.
-
Add permissions in info.plist file:
If picker mode is camera only, add camera usage description in plist file.
NSCameraUsageDescription
If picker mode is gallery only, add photo library usage description in plist file.
NSPhotoLibraryUsageDescription
For video recording, add microphone usage in plist file:
NSMicrophoneUsageDescription
-
You are now ready to use the Media Picker library.
On your button click, or place from where you want to open the picker:
let mediaPicker = MediaPickerViewController.getMediaPicker(with: .camera)
mediaPicker.1.mediaType = .all
mediaPicker.1.pickerMode = .all
mediaPicker.1.isCroppingEnabled = true
mediaPicker.1.delegate = self
mediaPicker.1.maximumImageSizeInMb = 3
mediaPicker.1.maximumVideoSizeInMb = 5
mediaPicker.0.modalTransitionStyle = .crossDissolve
mediaPicker.0.modalPresentationStyle = .custom
present(mediaPicker.0, animated: false, completion: nil)
Actionsheet can not be displayed in ipad so, we need to make UIPopOverController, to choose between camera and gallery when used in iPad.
Thus, add:
mediaPicker.1.sourceView = your button or view where the popover controller is to be presenter
Here
let mediaPicker = MediaPickerViewController.getMediaPicker(with: .camera)
returns {UINavigationcontroller, MediaPickerViewController
},
MediaPickerViewController
instance is the root of UINavigationcontroller
returned.
If you want a default configuration:
mediaPicker.1.setDefaultImagePickerConfiguration()
func setDefaultImagePickerConfiguration() {
pickerMode = .all
mediaType = .all
isCroppingEnabled = true
croppingStyle = .default
aspectRatioPreset = .presetSquare
aspectRatioLockEnabled = true
resetAspectRatioEnabled = false
rotateButtonsHidden = true
}
Now add delegate to get the selected media in your view controller:
extension ViewController: MediaPickerDelegate {
func didPickImage(_ image: UIImage) {
print("Image Picked")
}
func didClose(viewController: MediaPickerViewController) {
viewController.dismiss(animated: false, completion: nil)
}
func didPickVideo(_ withPath: URL) {
print("Did Pick Video")
}
func showErrorMessage(_ message: String) {
print(message)
}
}