I can load PHLivephoto
with loadTransferable(type: PHLivePhoto.self)
.
But I could not load livephoto loadTransferable(type: LivephotoTrasferable.self)
in which the LivephotoTrasferable
is as below:
struct LivephotoTrasferable: Transferable {
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(contentType: .livePhoto) { item in
SentTransferredFile(item.url)
} importing: { file in
print("livephoto!")
return Self.init(url: file.file)
}
}
var url: URL
}
The whole code snippet is:
struct ContentView: View {
@State private var selection: PhotosPickerItem?
var body: some View {
VStack {
PhotosPicker(selection: $selection) {
Text("Import image")
}
.buttonStyle(.borderedProminent)
.onChange(of: selection) { oldValue, newValue in
if let item = newValue {
self.loadTransferable(item)
self.selection = nil
}
}
}
.padding()
}
func loadTransferable(_ item: PhotosPickerItem) {
Task {
let livephoto = try? await item.loadTransferable(type: PHLivePhoto.self)
let livephoto2 = try? await item.loadTransferable(type: LivephotoTrasferable.self)
print(livephoto != nil, livephoto2 != nil) // true, false
}
}
}
struct LivephotoTrasferable: Transferable {
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(contentType: .livePhoto) { item in
SentTransferredFile(item.url)
} importing: { file in
print("livephoto!")
return Self.init(url: file.file)
}
}
var url: URL
}