iOS Development

ios – Easy methods to preview chosen video from the gallery

Spread the love


I’m attempting to show my chosen video as preview earlier than it uploaded, similar to my preview of the picture is being proven within the picture beneath the picture preview

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

Results of ‘VideoPlayer’ initializer is unused

 VideoPlayer(participant: AVPlayer(url: url))
 @State var url: URL?
 var physique: some View {
 if let url {
                            VideoPlayer(participant: AVPlayer(url: url))
                        }

Full Code

import AVKit
struct CreateView: View {
   
    @State var url: URL?
    non-public var person: Consumer? {
        return UserService.shared.currentUser
    }
    var physique: some View {
        NavigationStack {
            
                VStack {
                    HStack(alignment: .high) {
                        RectangularImageSize( measurement: .small)
                        
                        VStack(alignment: .main, spacing: 4) {
                            Textual content(person?.username ?? "")
                                .fontWeight(.semibold)
                            
                            TextField("Sort 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: "photograph")
                                    }
                                
                            }
                            
                        }
                        
                        
                        Spacer()
                    }
                    .padding()
                    .toolbar {
                        ToolbarItem(placement: .navigationBarLeading) {
                            Button("Cancel") {
                                dismiss()
                            }
                            .font(.subheadline)
                            .foregroundStyle(Shade.black)
                        }
                        
                        ToolbarItem(placement: .navigationBarTrailing) {
                            Button("Submit") {
                                Process {
                                    attempt 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) { lead to
                Process {
                    do {
                        if let information = attempt await viewModel.selectedImage?.loadTransferable(kind: Knowledge.self) {
                            if let uiImage = UIImage(information: information) {
                                self.uiImage = uiImage
                                if let url {
                                    
                                }
                            }
                        }
                    } catch {
                        print(error.localizedDescription)
                        viewModel.selectedImage = nil
                    }
                        if let url {
                            VideoPlayer(participant: AVPlayer(url: url))
                        }
                    

Leave a Reply

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