-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
61 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
Sources/GravatarUI/SwiftUI/AvatarPicker/SystemImagePicker/CameraImagePicker.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import PhotosUI | ||
import SwiftUI | ||
|
||
struct CameraImagePicker: UIViewControllerRepresentable { | ||
let onImageSelected: (ImagePickerItem) -> Void | ||
|
||
@State private var pickedImage: ImagePickerItem? { | ||
didSet { | ||
if let pickedImage { | ||
onImageSelected(pickedImage) | ||
} | ||
} | ||
} | ||
|
||
func makeUIViewController(context: Context) -> UIImagePickerController { | ||
let imagePicker = UIImagePickerController() | ||
// Forcefully use UIImagePickerController for device camera only | ||
imagePicker.sourceType = .camera | ||
imagePicker.delegate = context.coordinator | ||
// Use custom image cropper | ||
imagePicker.allowsEditing = false | ||
|
||
return imagePicker | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { | ||
// No-op | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
} | ||
|
||
final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { | ||
var parent: CameraImagePicker | ||
|
||
init(_ parent: CameraImagePicker) { | ||
self.parent = parent | ||
} | ||
|
||
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { | ||
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage { | ||
parent.pickedImage = .init(id: UUID().uuidString, image: image) | ||
} | ||
} | ||
|
||
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | ||
picker.dismiss(animated: true) | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
Sources/GravatarUI/SwiftUI/AvatarPicker/SystemImagePicker/PhotosImagePicker.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import PhotosUI | ||
import SwiftUI | ||
|
||
struct PhotosImagePicker: UIViewControllerRepresentable { | ||
let onImageSelected: (ImagePickerItem) -> Void | ||
let onCancel: () -> Void | ||
|
||
@State private var pickedImage: ImagePickerItem? { | ||
didSet { | ||
if let pickedImage { | ||
onImageSelected(pickedImage) | ||
} | ||
} | ||
} | ||
|
||
func makeUIViewController(context: Context) -> PHPickerViewController { | ||
var config = PHPickerConfiguration() | ||
config.filter = .images | ||
config.selectionLimit = 1 | ||
let picker = PHPickerViewController(configuration: config) | ||
picker.delegate = context.coordinator | ||
return picker | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { | ||
// No-op | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
} | ||
|
||
final class Coordinator: NSObject, PHPickerViewControllerDelegate { | ||
let parent: PhotosImagePicker | ||
|
||
init(_ parent: PhotosImagePicker) { | ||
self.parent = parent | ||
} | ||
|
||
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { | ||
guard | ||
let result = results.first, | ||
result.itemProvider.canLoadObject(ofClass: UIImage.self) | ||
else { | ||
parent.onCancel() | ||
return | ||
} | ||
|
||
Task { | ||
let image = await result.itemProvider.loadUIImage() | ||
let imageItem = ImagePickerItem(id: UUID().uuidString, image: image) | ||
parent.pickedImage = imageItem | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension NSItemProvider { | ||
fileprivate func loadUIImage() async -> UIImage { | ||
await withCheckedContinuation { continuation in | ||
loadObject(ofClass: UIImage.self) { itemReading, _ in | ||
guard let image = itemReading as? UIImage else { | ||
return | ||
} | ||
|
||
continuation.resume(returning: image) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters