Reputation: 7584
I'm using a PHPickerViewController
to allow the user to select a media item from their library. And the specific case of interest is when they select a video.
I get a callback on the PHPickerViewControllerDelegate
delegate method:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
And I'm using loadFileRepresentation
to get the video from the NSItemProvider
.
However, that call takes a substantial amount of time. And it may be all for nothing -- since I'm imposing a size limit on the videos that users can add to the app.
So, my question is: Is there a way to determine, quickly, the size of the media item that the user has selected? E.g., given the NSItemProvider
?
Upvotes: 1
Views: 1209
Reputation: 109
I would also try loadInPlaceFileRepresentation if possible, it will be much faster.
Upvotes: 0
Reputation: 535890
You're looking in the wrong place. What you're starting with, even before you have an item provider, is a PHPickerResult. It gives you the asset identifier. From there you can obtain from the photo library the PHAsset and find out anything the photo library is willing to tell you. Size is an amorphous concept, but you can certainly get the video pixel dimensions and its duration instantly, which will tell you a lot.
Of course you'll need photo library read permission to go that route, which you don't need merely to save the video to a file.
On the other hand, since videos are huge, one might ask what you care about the size. The key thing to do with a video is not to obtain its entirety as a lump of data but to play it, and the photo library will let you do that instantly.
Upvotes: 2