[ad_1]
I’m attempting to preview my video earlier than its uploaded the identical manner the imageview is being on show to be preview
I’m not in a position to show my chosen video in display screen as preview due to these errors.
‘nil’ requires a contextual sort
viewModel.selectedImage = nil
‘nil’ can’t be assigned to sort ‘[Video]’
viewModel.movies = nil
I must show my chosen video as preview, similar to my preview of the picture is being proven in my above picture.
That is my code
.onChange(of: viewModel.selectedImage) { lead to
Job {
do {
if let knowledge = strive await viewModel.selectedImage?.loadTransferable(sort: Information.self) {
if let uiImage = UIImage(knowledge: knowledge) {
self.uiImage = uiImage
if let url {
}
}
}
} catch {
print(error.localizedDescription)
viewModel.selectedImage = nil
.onChange(of: viewModel.movies) { lead to
Job {
do {
if let knowledge = strive await viewModel.movies {
VideoPlayer(participant: AVPlayer(url: url!))
}
} catch {
print(error.localizedDescription)
viewModel.movies = nil
}
}
import SwiftUI
import PhotosUI
import Kingfisher
import AVKit
struct CreateView: View {
@State non-public var selectedPhoto: PhotosPickerItem?
@State non-public var picture: Picture?
@State var captionText = ""
@State var imagePickerPresented = false
@StateObject var viewModel = UploadPostViewModel()
@Atmosphere(.dismiss) var dismiss
@State var uiImage: UIImage?
@Binding var tabIndex: Int
@State var url: URL?
non-public var person: Customers? {
return UserService.shared.currentUser
}
var physique: some View {
NavigationStack {
ZStack{
(LinearGradient(gradient: Gradient(colours: [Color.white, Color.gray]), startPoint: .prime, endPoint: .backside))
.ignoresSafeArea()
VStack {
HStack(alignment: .prime) {
RectangularImageSize( dimension: .small)
VStack(alignment: .main, spacing: 4) {
Textual content(person?.username ?? "")
.fontWeight(.semibold)
TextField("Kind One thing", textual content: $viewModel.textual content, axis: .vertical)
Spacer()
}
.font(.footnote)
if !viewModel.textual content.isEmpty {
Button {
viewModel.textual content = ""
} label: {
Picture(systemName: "xmark")
.resizable()
.body(width: 12, peak: 12)
.foregroundColor(.grey)
Button(motion: {
viewModel.selectedImage = nil
}, label: {
Picture(systemName: "xmark")
.resizable()
.scaledToFit()
.body(width: 10, peak: 10)
.padding(8)
})
.background(Colour(.grey))
.foregroundColor(.white)
.clipShape(Circle())
}
.padding(8)
Spacer()
} else {
if viewModel.textual content.isEmpty {
PhotosPicker(
choice: $viewModel.selectedImage,
matching: .any(of: [.images,.videos, .not(.livePhotos)]),
photoLibrary: .shared()) {
Picture(systemName: "photograph")
}
}
}
Spacer()
}
.padding()
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
dismiss()
}
.font(.subheadline)
.foregroundStyle(Colour.black)
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("Submit") {
Job {
strive await viewModel.uploadPost(caption: captionText)
dismiss()
}
}
.opacity(viewModel.textual content.isEmpty ? 0.5 : 1.0)
.disabled(viewModel.textual content.isEmpty)
.font(.subheadline)
.fontWeight(.semibold)
.foregroundStyle(Colour.inexperienced)
}
}
.onDisappear { tabIndex = 0 }
}
Divider()
if uiImage != nil {
Picture(uiImage: uiImage!)
.resizable()
.scaledToFit()
.body(width: 200, peak: 200)
}
}
.onChange(of: viewModel.selectedImage) { lead to
Job {
do {
if let knowledge = strive await viewModel.selectedImage?.loadTransferable(sort: Information.self) {
if let uiImage = UIImage(knowledge: knowledge) {
self.uiImage = uiImage
if let url {
}
}
}
} catch {
print(error.localizedDescription)
viewModel.selectedImage = nil
.onChange(of: viewModel.movies) { lead to
Job {
do {
if let knowledge = strive await viewModel.movies {
VideoPlayer(participant: AVPlayer(url: url!))
}
} catch {
print(error.localizedDescription)
viewModel.movies = nil
}
[ad_2]