Swift: How to Handle Video and Photo Results from a PHPicker

Swift: How to handle video and photo results from a PHPicker?

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)
}

How to fetch live photo or video from PHPickerViewController delegate

Live photos are easy; do it exactly the same way. You can do that because PHLivePhoto is a class. So:

    let prov = result.itemProvider
prov.loadObject(ofClass: PHLivePhoto.self) { livePhoto, err in
if let photo = livePhoto as? PHLivePhoto {
DispatchQueue.main.async {
// display the live photo here

Videos are harder. The problem is that you do not want to be handed the data; it does you no good and is likely to be huge. You want the data saved to disk so that you can access the video URL, just like what UIImagePickerController used to do. You can in fact ask the item provider to save its data for you, but it wants to let go of that data when the completion handler returns. My solution is to access the URL in a .sync function:

    let prov = result.itemProvider
prov.loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier) { url, err in
if let url = url {
DispatchQueue.main.sync {
// display the video here

How to retrieve PHAsset from PHPicker?

I managed to find an answer from the developers of this framework on the apple forum:

Yes, PHPickerResult has the assetIdentifier property which can contain
a local identifier to fetch the PHAsset from the library. To have
PHPicker return asset identifiers, you need to initialize
PHPickerConfiguration with the library.

Please note that PHPicker does not extend the Limited Photos Library
access for the selected items if the user put your app in Limited
Photos Library mode. It would be a good opportunity to reconsider if
the app really needs direct Photos Library access or can work with
just the image and video data. But that really depend on the app.

The relevant section of the "Meet the new Photos picker" session
begins at 10m 20s.

Sample code for PhotoKit access looks like this:

import UIKit
import PhotosUI
class PhotoKitPickerViewController: UIViewController, PHPickerViewControllerDelegate {
@IBAction func presentPicker(_ sender: Any) {
let photoLibrary = PHPhotoLibrary.shared()
let configuration = PHPickerConfiguration(photoLibrary: photoLibrary)
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true)
}

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)

let identifiers = results.compactMap(\.assetIdentifier)
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: nil)

// TODO: Do something with the fetch result if you have Photos Library access
}
}

PHPickerViewController add video editing screen aka (UIImagePickerController.allowsEditing = true)

I investigated this issue but indeed there is no support for like as you mention allowsediting.

For example related to your question preset values for video edits was depreciated to UIImagePickerController.ImageURLExportPreset.compatible but There is no support for automatic compression. The picker will always pass the original video/image and it is up to the app to do the necessary compressions or edits.
You can check this Apple Document: imageExportPreset.

Apple specifically mentions that we should be using this new one instead of the older UIImagePickerViewController. If someone wonder more : Meet the new Photos picker



Related Topics



Leave a reply



Submit