fmodf
b3b3b3aef7
Reviewed-on: narayana/conversations-classic-ios#1 Co-authored-by: fmodf <fmodf.ios@gmail.com> Co-committed-by: fmodf <fmodf.ios@gmail.com>
50 lines
1.8 KiB
Swift
50 lines
1.8 KiB
Swift
import Foundation
|
|
import Photos
|
|
import SwiftUI
|
|
|
|
struct CameraPicker: UIViewControllerRepresentable {
|
|
var completionHandler: (Data, GalleryMediaType) -> Void
|
|
|
|
func makeUIViewController(context: Context) -> UIImagePickerController {
|
|
let picker = UIImagePickerController()
|
|
picker.sourceType = .camera
|
|
picker.delegate = context.coordinator
|
|
picker.mediaTypes = [UTType.movie.identifier, UTType.image.identifier]
|
|
picker.videoQuality = .typeHigh
|
|
picker.videoMaximumDuration = Const.videoDurationLimit
|
|
picker.view.backgroundColor = .clear
|
|
return picker
|
|
}
|
|
|
|
func updateUIViewController(_: UIImagePickerController, context _: Context) {}
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
Coordinator(self)
|
|
}
|
|
|
|
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
|
|
let parent: CameraPicker
|
|
|
|
init(_ parent: CameraPicker) {
|
|
self.parent = parent
|
|
}
|
|
|
|
func imagePickerController(_: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
|
|
// swiftlint:disable:next force_cast
|
|
let mediaType = info[.mediaType] as! String
|
|
|
|
if mediaType == UTType.image.identifier {
|
|
if let image = info[.originalImage] as? UIImage {
|
|
let data = image.jpegData(compressionQuality: 1.0) ?? Data()
|
|
parent.completionHandler(data, .photo)
|
|
}
|
|
} else if mediaType == UTType.movie.identifier {
|
|
if let url = info[.mediaURL] as? URL {
|
|
let data = try? Data(contentsOf: url)
|
|
parent.completionHandler(data ?? Data(), .video)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|