Proof of concept of using PHPicker
in SwiftUI. See https://codakuma.com/the-library-is-open for a full tutorial.
Last active
July 4, 2022 11:31
-
-
Save shaundon/56c8a5801c5dc63c2e4a0af9b39c8f7b to your computer and use it in GitHub Desktop.
PHPicker in SwiftUI
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
import SwiftUI | |
struct ContentView: View { | |
@State private var showPhotoSheet = false | |
@State private var image: UIImage? = nil | |
var body: some View { | |
VStack { | |
Button(action: { showPhotoSheet = true }) { | |
Label("Choose photo", systemImage: "photo.fill") | |
} | |
.fullScreenCover(isPresented: $showPhotoSheet) { | |
PhotoPicker(filter: .images, limit: 1) { results in | |
PhotoPicker.convertToUIImageArray(fromResults: results) { (imagesOrNil, errorOrNil) in | |
if let error = errorOrNil { | |
print(error) | |
} | |
if let images = imagesOrNil { | |
if let first = images.first { | |
print(first) | |
image = first | |
} | |
} | |
} | |
} | |
.edgesIgnoringSafeArea(.all) | |
} | |
if let image = image { | |
Image(uiImage: image) | |
.resizable() | |
.aspectRatio(contentMode: .fit) | |
.frame(maxWidth: 200, maxHeight: 200) | |
} | |
} | |
} | |
} |
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
import SwiftUI | |
import PhotosUI | |
struct PhotoPicker: UIViewControllerRepresentable { | |
typealias UIViewControllerType = PHPickerViewController | |
let filter: PHPickerFilter | |
var limit: Int = 0 // 0 == 'no limit'. | |
let onComplete: ([PHPickerResult]) -> Void | |
func makeUIViewController(context: Context) -> PHPickerViewController { | |
var configuration = PHPickerConfiguration() | |
configuration.filter = filter | |
configuration.selectionLimit = limit | |
let controller = PHPickerViewController(configuration: configuration) | |
controller.delegate = context.coordinator | |
return controller | |
} | |
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {} | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
class Coordinator: PHPickerViewControllerDelegate { | |
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { | |
parent.onComplete(results) | |
picker.dismiss(animated: true) | |
} | |
private let parent: PhotoPicker | |
init(_ parent: PhotoPicker) { | |
self.parent = parent | |
} | |
} | |
static func convertToUIImageArray(fromResults results: [PHPickerResult], onComplete: @escaping ([UIImage]?, Error?) -> Void) { | |
var images = [UIImage]() | |
let dispatchGroup = DispatchGroup() | |
for result in results { | |
dispatchGroup.enter() | |
let itemProvider = result.itemProvider | |
if itemProvider.canLoadObject(ofClass: UIImage.self) { | |
itemProvider.loadObject(ofClass: UIImage.self) { (imageOrNil, errorOrNil) in | |
if let error = errorOrNil { | |
onComplete(nil, error) | |
dispatchGroup.leave() | |
} | |
if let image = imageOrNil as? UIImage { | |
images.append(image) | |
dispatchGroup.leave() | |
} | |
} | |
} | |
} | |
dispatchGroup.notify(queue: .main) { | |
onComplete(images, nil) | |
} | |
} | |
} |
I’m no expert but it looks like the photo being chosen isn’t actually there – how does it perform on a real device (not the simulator)?
Looks like a bug in the simulator. I can see exactly the same error in the simulator when I pick the top left standard image (even the UUID is exactly the same as above). Any other image is picked successfully. No such issue seen on the device. Neither it is seen in the simulator when UIImagePickerController is used instead.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i can see this kind of error