Как загрузить живое фото из PhotosPickerItem с помощью TransferableIOS

Программируем под IOS
Anonymous
Как загрузить живое фото из PhotosPickerItem с помощью Transferable

Сообщение Anonymous »

Я могу загрузить PHLivephoto с помощью loadTransferable(type: PHLivePhoto.self).
Но мне не удалось загрузить livephoto loadTransferable(type: LivephotoTrasferable.self)
в котором LivephotoTrasferable выглядит следующим образом:
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
}

Весь фрагмент кода:
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
}



Подробнее здесь: https://stackoverflow.com/questions/783 ... ansferable

Вернуться в «IOS»