I know that UIImagePickerController allows you to compress media content upon selection. However, I'm not sure how to do the same with PHPickerViewController. How can I compress a video after getting a result (didFinishPicking), (so I can reduce the amount of time to upload and download the data from then on)
- 55
- 9
1 Answers
I don't think PHPickerViewController does that, but there are other ways to do it depending on how you work with the picker:
If you're using the default picker configuration (i.e. creating a
PHPickerConfigurationwithout passing the photo library and therefore not having the need to request permissions), you'll have to work withNSItemProvider. From what I learned, the only option might be to save the video to a temporary file using the item provider and the convert it to a lower resolution/bitrate usingAVAssetExportSession(see some examples here: How can I reduce the file size of a video created with UIImagePickerController?).If you are using the picker with an explicitly passed photo library, the
PHPickerResultwill have theassetIdentifierprovided to you, and you can use it to get a respectivePHAsset. Then, usePHImageManager'srequestAVAssetmethod to get a video asset of a desired quality. You'll still need to export theAVAssetviaAVAssetExportSessionthough, but with this approach you won't have a (potentially large) full-res temporary file on disk. I was recently working on a open source app that uses this technique, you can find some examples here: https://github.com/vadimbelyaev/TbilisiCleanups/blob/main/TbilisiCleanups/Services/MediaUploadService.swift
- 2,456
- 2
- 18
- 23
-
Thank you for this advice! My only worries are that it'll take longer to download and re-download the compress version, then upload to database, it seems like quite a process. – EAO123 Aug 23 '22 at 09:14