Это моя передаваемая структура.
Код: Выделить всё
struct Movie: Transferable {
let url: URL
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(contentType: .movie) { movie in
SentTransferredFile(movie.url)
} importing: { received in
let copy = URL.documentsDirectory.appending(path: "movie.mp4")
if FileManager.default.fileExists(atPath: copy.path()) {
try FileManager.default.removeItem(at: copy)
}
try FileManager.default.copyItem(at: received.file, to: copy)
return Self.init(url: copy)
}
}
}
Код: Выделить всё
struct GalleryView: View {
@State private var selectedItem: PhotosPickerItem?
....
// Somewhere in ZStack of GalleryView
HStack {
Spacer()
VStack {
Spacer()
PhotosPicker(
selection: $selectedItem,
matching: .videos)
{
Image("btn_plus")
}
.padding(.bottom, 16)
}
.padding(.trailing, 16)
}
..........
.onChange(of: selectedItem) { newValue in
Task {
do {
print("loading!!!...")
if let movie = try await selectedItem?.loadTransferable(type: Movie.self) {
print("loaded!!!...")
} else {
print("failed!!!...")
}
} catch {
print("failed with ex!!!...")
}
}
}
Код: Выделить всё
loading!!!...
Подробнее здесь: https://stackoverflow.com/questions/784 ... ge-swiftui