要区分UIImagePickerViewController
's 与相同sourceType
,您可以引入一个新实体Role
:
fileprivate enum ImagePickerRole {
case avatar
case chooseImage
case takePhoto
}
fileprivate class ImagePicker: UIImagePickerController {
private(set) var role: ImagePickerRole = .takePhoto
}
fileprivate extension ImagePicker {
convenience init(role imagePickerRole: ImagePickerRole) {
self.init()
role = imagePickerRole
switch role {
case .avatar, .takePhoto:
sourceType = .camera
cameraCaptureMode = .photo
showsCameraControls = true
case .chooseImage:
sourceType = .photoLibrary
}
}
在您的进一步,您UIImagePickerViewControllerDelegate
可以检查控制器的角色:
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let imagePicker = picker as? ImagePicker else { return }
switch imagePicker.role {
case .avatar:
// Your code here
case .takePhoto:
// Your code here
case .chooseImage:
// Your code here
}
}