I'm using this code to pick video or image using PhotosPicker:
PhotosPicker(
selection: $selectedItem,
matching: matching: .any(of: [.images, .videos]),
photoLibrary: .shared()) {
Text("edit")
.padding(.horizontal, 18)
.padding(.vertical, 2)
.background(Color.black.opacity(0.5))
.cornerRadius(12)
}
.onChange(of: selectedItem) { newItem in
Task {
if let data = try? await newItem?.loadTransferable(type: Data.self) {
selectedFileData = data
// mimeType = ???
}
}
}
How can I find out the mime Type from the selected file? I know how to get it when I have the path/url of the selected file but I don't know how to get it with PhotosPicker either
Struggling with the same issue.
It looks like we can access var supportedContentTypes: [UTType] { get } from PhotosPickerItem
The content types the item supports in order of the most preferred to the least.
https://developer.apple.com/documentation/photokit/photospickeritem/4022920-supportedcontenttypes
item.supportedContentTypes.first?.preferredMIMEType // Optional("image/jpeg")
Better than nothing I guess.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With