iOS Development

ios – preview chosen video earlier than importing it

Spread the love


I’m attempting to preview my video earlier than its uploaded the identical method the imageview is being on show to be preview

I’m not in a position to show my chosen video in display as preview due to these errors.

‘nil’ requires a contextual kind

 viewModel.selectedImage = nil

‘nil’ can’t be assigned to kind ‘[Video]’

viewModel.movies = nil

I must show my chosen video as preview, identical to my preview of the picture is being proven in my above picture.

That is my code

 .onChange(of: viewModel.selectedImage) { end in
                Activity {
                    do {
                        if let knowledge = strive await viewModel.selectedImage?.loadTransferable(kind: 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) { end in
                                Activity {
                                    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: .high, endPoint: .backside))
                    .ignoresSafeArea()
                VStack {
                    HStack(alignment: .high) {
                        RectangularImageSize( measurement: .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, top: 12)
                                    .foregroundColor(.grey)
                                
                                
                                Button(motion: {
                                    viewModel.selectedImage = nil
                                }, label: {
                                    Picture(systemName: "xmark")
                                        .resizable()
                                        .scaledToFit()
                                        .body(width: 10, top: 10)
                                        .padding(8)
                                })
                                .background(Shade(.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: "picture")
                                    }
                                
                            }
                            
                        }
                        
                        
                        Spacer()
                    }
                    .padding()
                    .toolbar {
                        ToolbarItem(placement: .navigationBarLeading) {
                            Button("Cancel") {
                                dismiss()
                            }
                            .font(.subheadline)
                            .foregroundStyle(Shade.black)
                        }
                        
                        ToolbarItem(placement: .navigationBarTrailing) {
                            Button("Put up") {
                                Activity {
                                    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(Shade.inexperienced)
                        }
                    }
                    .onDisappear { tabIndex = 0 }
                    
                }
                Divider()
                if uiImage != nil {
                    Picture(uiImage: uiImage!)
                        .resizable()
                        .scaledToFit()
                        .body(width: 200, top: 200)
                }
            }
            .onChange(of: viewModel.selectedImage) { end in
                Activity {
                    do {
                        if let knowledge = strive await viewModel.selectedImage?.loadTransferable(kind: 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) { end in
                                Activity {
                                    do {
                                        if let knowledge = strive await viewModel.movies {
                                            VideoPlayer(participant: AVPlayer(url: url!))
                                            
                                            
                                            
                                        }
                                    } catch {
                                        print(error.localizedDescription)
                                        viewModel.movies = nil
                                    }

Leave a Reply

Your email address will not be published. Required fields are marked *