Learn Courses My Dashboard

Share data from image picker to other file/view

Hi, I made this image picker bellow. The goal is to let the user choose pictures from their library and display these in a tabview as bellow in another file/ view, but you can’t change the tabview in the new view. I want to use the data and the images that has been selected in an other view. How can I do this? How can I refrence selectedImages in my other view/file (called it FeedView)? I only get an error saying can’t find selectedImages in scope. Thank you for feedback.

import PhotosUI
import SwiftUI

struct ProfileView: View {

@State private var selectedItems = [PhotosPickerItem]()
@State private var selectedImages = [UIImage]()
   

var body: some View {
   
   
        VStack{
            
            Text("Edit Profile")
                .frame(height: 30)
                .font(.system(size: 30))
                
                    if selectedImages.count > 0 {
                        ZStack{
                        

                            
                            VStack{
                                TabView {
                                    ForEach(selectedImages, id: \.self) { img in
                                        ZStack{
                                            Image(uiImage: img)
                                                .resizable()
                                               
                                            
                                        }
                                    }
                                }
                            .tabViewStyle(.page(indexDisplayMode: .always))
                            .frame(width: 320, height: 400)
                            .cornerRadius(20)
                                
                               
                                
                            }
                }
                    
                    } else {
                        ZStack{
                            
                            
                            
                            VStack{
                                Image(systemName: "photo.artframe")
                                    .resizable()
                                    .frame(width: 320, height: 400)
                                    .foregroundColor(.black)
                                    .padding()
                            
                                
                                
                               
                        }
                    }
                

            }
            PhotosPicker(selection: $selectedItems, maxSelectionCount: 6, matching: .any(of: [.images,.not(.videos)])) {
                Label("Pick profile pictures", systemImage: "photo.artframe")
            }
           
            .onChange(of: selectedItems) { newValues in
                Task {
                    selectedImages = []
                    for value in newValues {
                        if let imageData = try? await value.loadTransferable(type: Data.self), let image = UIImage(data: imageData) {
                            selectedImages.append(image)
                            
                            
                        
                    }
                }
            }
        }
    
    }
}

}

struct ProfileView_Previews: PreviewProvider {
static var previews: some View {
ProfileView()
}
}

@erikspo

When you call your FeedView you need to pass in the selectedImages as a Binding. In other words in your FeedView declare a @Binding var selectedImages: [UIImage]

and then when you call FeedView you would say something like:

FeedView(selectedImages: selectedImages)

Thank you!

Hi I tried this code, but got an error saying can’t find selectedImages in scope when I used the @binding var selectedImages: [UIImage]. You wouldn’t happen to know why that is? Included the code if that helps to answer the question (error is with the preview_provider:

import SwiftUI

struct FeedView: View {

@Binding var selectedImages: [UIImage]

var body: some View {
    ZStack {
    
        
        
       
        
        VStack{
            TabView {
                ForEach(selectedImages, id: \.self) { img in
                    ZStack{
                        Image(uiImage: img)
                            .resizable()
                           
                        
                    }
                }
            }

                .tabViewStyle(.page(indexDisplayMode: .never))
                .frame(width: 400, height: 500)
                .cornerRadius(20)
                
                
                
            
                
                }
                    
                
           
        
        
               }
            }
        }

struct FeedView_Previews: PreviewProvider {
static var previews: some View {
FeedView(selectedImages: selectedImages)
//Cannot find ‘selectedImages’ in scope
}
}

You need to give the preview a variable to use for selectedImages that’s why it’s not found because in that struct it doesn’t exist

You can see the FeedView_Previews is it’s own struct, that inherits from PreviewProvider

But you almost always need to pass in or use some kind of variables to make the previews work.

Yours should look something like this:

Also I used an empty array but if you want your preview to be populated you’d need an array of UIImage rather than an empty array

struct FeedView_Previews: PreviewProvider {
static var previews: some View {
   FeedView(selectedImages: .constant([]))
}
1 Like