1

I need the ability for a user to select multiple photos and videos from there photo library. (Using PHPicker) I already know how to get images with this:

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    var contentsData: [Data] = []
    for result in results {
        if result.itemProvider.canLoadObject(ofClass: UIImage.self) {
            result.itemProvider.loadObject(ofClass: UIImage.self) { selectedData, error in
                if let error = error {
                    print("PICKER ERROR: \(error)")
                } else {
                    if let selectedImage = selectedData as? UIImage {
                        if let selectedImageData = selectedImage.jpegData(compressionQuality: 0.5) {
                            contentsData.append(selectedImageData)
                            if contentsData.count == results.count {
                                self.parent.completion(contentsData)
                            }
                        }
                    }
                }
            }
        }
    }
    self.parent.presentationMode.wrappedValue.dismiss()
}

I would assume that what I would need to do is check what data type each result from PHPickerResult is and then load each object accordingly. The problem is that a video file could be .mov, .mp4, etc. How can I identify a PHPickerResult as a video and handle it accordingly?

Trevor
  • 580
  • 5
  • 16

1 Answers1

7

Just ask whether the item provider has an item of type UTType.movie.identifier. If so, go ahead and load it by calling loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier).

Actual code:

    if prov.hasItemConformingToTypeIdentifier(UTType.movie.identifier) {
        self.dealWithVideo(result)
    } else if prov.canLoadObject(ofClass: PHLivePhoto.self) {
        self.dealWithLivePhoto(result)
    } else if prov.canLoadObject(ofClass: UIImage.self) {
        self.dealWithImage(result)
    }
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 3
    See https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch17p697pickaMovieOrPhoto/ch30p960pickaMovie/ViewController.swift – matt Mar 07 '22 at 00:05
  • Perfect, I like that it uses canLoad to distinguish the data types. – Trevor Mar 07 '22 at 01:43
  • I can't get a video to load, have looked at the iOS-Book-Example, have created a repo to demonstrate/test: https://github.com/ricsantos/PhotoLibraryPickerTest – Ric Santos Mar 24 '23 at 03:30
  • @RicSantos Yes, I can see what you're doing wrong. It has nothing to do with the picker or what I said in my answer. You are making two very basic FileManager mistakes, that's all. Trivially easy to fix: I changed two lines of your code and the video loads and is playable (after I added the missing wrap up call at the end, of course). – matt Mar 24 '23 at 13:51
  • Thanks @matt I had a look with fresh eyes and can see the error of my ways! Wasn't using url.path and wasn't creating a directory before copying to it. Doh! – Ric Santos Mar 27 '23 at 22:15