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
3
Answers
By passing the reference URL path to this method you can get picked image or video MIME type
I hope it may help you…
Struggling with the same issue.
It looks like we can access
var supportedContentTypes: [UTType] { get }
fromPhotosPickerItem
https://developer.apple.com/documentation/photokit/photospickeritem/4022920-supportedcontenttypes
Better than nothing I guess.
I found a way. You can check item supported type and custom handle. Try replace your Task in onChange to this: