显示照片库中的图像

问题描述 投票:0回答:0

我想显示从 PhotoLibrary 中选择的图像。 ImagePicker 调出 PhotoLibrary,我可以选择它,但它不会显示在视图中。 我有一个占位符,用于显示带有 Image(uiImage: self.image) 和 frame 50x50 的图像。 如何更新当前视图中的图像

struct MyView: View {
    @State private var isShowPhotoLibrary = false
    @State private var image = UIImage()

    var body: some View {
        NavigationView {
        VStack (alignment: .leading){
            TextField("Full Name", text: $nameInput)
                .textContentType(.name)
                
            TextField("Phone Number", text: $phoneInput)
                .textContentType(.telephoneNumber)
            Spacer()
            
            Image(uiImage: self.image)
                .resizable()
                .scaledToFill()
                .frame(minWidth: 50, maxWidth: 50)
               // .edgesIgnoringSafeArea(.all)

                Button(action: {
                    self.isShowPhotoLibrary = true
                }) {
                    HStack {
                        Image(systemName: "photo")
                            .font(.system(size: 20))
                            
                        Text("Select Photo")
                    }
                    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 50)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(20)
                    .padding(.horizontal)
                }

            .sheet(isPresented: $isShowPhotoLibrary) {
                ImagePicker(sourceType: .photoLibrary, selectedImage: self.$image)
            }
            
        
        }

        .padding()
        .navigationTitle("Add")

    }
   
}
ios swiftui uiimage
© www.soinside.com 2019 - 2024. All rights reserved.