Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SwiftUI - PhotosPicker getting mime type from selected file

Tags:

ios

swift

swiftui

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

like image 991
Coddo Avatar asked Dec 05 '25 14:12

Coddo


1 Answers

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.

like image 74
kironet Avatar answered Dec 07 '25 06:12

kironet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!