How load Video with New(iOS 16) PhotosPicker? loadTransferable(type:)

I use loadFileRepresentation(forTypeIdentifier:completionHandler:) to load video with PHPickerViewController

What can I use load video with?

// my sample code

func loadPhoto(pickerItem: PhotosPickerItem) async throws -> Photo {

    if let livePhoto = try await pickerItem.loadTransferable(type: PHLivePhoto.self) {

      let photo: Photo = .init(id: pickerItem.itemIdentifier, item: livePhoto)

      return photo

    } else if let url = try await pickerItem.loadTransferable(type: URL.self) {

      let photo: Photo = .init(id: pickerItem.itemIdentifier, item: url)

      return photo

    } else if let data = try await pickerItem.loadTransferable(type: Data.self) {

      let photo: Photo = .init(id: pickerItem.itemIdentifier, item: data)

      return photo

    }



    throw PhotoError.load

  }
Answered by zunda in 723283022

This problem fixed in iOS 16 beta 5. thanks

https://github.com/zunda-pixel/SamplePhotosPicker

You should define you own model object conforming to the Transferable protocol. For example:

struct Movie: Transferable {
    let url: URL
    static var transferRepresentation: some TransferRepresentation {
        FileRepresentation(contentType: .movie) { movie in
            SentTransferredFile($0.url)
        } importing: { received in
            let copy: URL = URL(fileURLWithPath: "<#...#>")
            try FileManager.default.copyItem(at: received.file, to: copy)
            return Self.init(url: copy)
        }
    }
}

Then you can write:

let movie = try await pickerItem.loadTransferable(type: Movie.self)

For more information: https://developer.apple.com/documentation/coretransferable/filerepresentation

FileRepresentation.importing's 'receivedData' is already deleted. when I use.

import CoreTransferable

struct Movie: Transferable {

  let url: URL

  static var transferRepresentation: some TransferRepresentation {
    FileRepresentation(contentType: .movie) { movie in
      SentTransferredFile(movie.url)
    } importing: { receivedData in
      let fileName = receivedData.file.lastPathComponent

      let copy: URL =  FileManager.default.temporaryDirectory.appendingPathComponent(fileName)

      try FileManager.default.copyItem(at: receivedData.file, to: copy)

      return .init(url: copy)
    }
  }
}
Error Domain=NSCocoaErrorDomain Code=260 "The file “IMG_0458.mov” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/private/var/mobile/Containers/Data/Application/D2E2AF9F-E879-4792-89CE-E7FB6B1B8234/tmp/.com.apple.Foundation.NSItemProvider.flbzCW/IMG_0458.mov, NSUnderlyingError=0x28389eeb0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Accepted Answer

This problem fixed in iOS 16 beta 5. thanks

https://github.com/zunda-pixel/SamplePhotosPicker

How load Video with New(iOS 16) PhotosPicker? loadTransferable(type:)
 
 
Q