In iOS 14 Apple has introduced PHPickerViewController
where user has access to provide permission to all photo library videos or selected videos.
In first case when we provide permission to all videos,
we are able to get videos from photo library and able to convert it into the video-data to send it to backend server.
But in second case when user provide permission to selected videos, In this scenario we are able to get the videos from the photo library ,but unable to convert it into the data from local video url.At that time data is always getting nil.
We have used below code to retrieve video from photo library url and converted it into the data.
// MARK: PHPickerViewControllerDelegate Methods
extension PhotoPickerVC: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
// Always dismiss the picker first
dismiss(animated: true)
if !results.isEmpty {
guard let itemProvider = results.first?.itemProvider else { return }
itemProvider.loadItem(forTypeIdentifier: "public.movie", options: nil) { [weak self] (fileURL, _) in
DispatchQueue.main.async {
guard let videoURL = fileURL as? URL, let _ = self else { return }
do {
//mediaURL video loading
print(videoURL)
let VideoData = try Data(contentsOf: videoURL, options: Data.ReadingOptions.alwaysMapped)
print(VideoData)
} catch _ {
print("Received nil VideoData")
}
}
}
}
}
}