【问题标题】:How to view a selected image from imagepicker如何从图像选择器中查看选定的图像
【发布时间】:2023-01-24 03:45:21
【问题描述】:

I need help rewriting this code my selected image is not replacing my button that allow the user to pick the image

我尝试使用此代码来解决问题,但它根本不允许查看我的图像选择器,我收到了以下错误:

 ImagePicker(sourceType: .photoLibrary, selectedImage: self.$image)

无法参考成员“photoLibrary”推断上下文基础

调用中位置#1、#2 的额外参数

调用中缺少参数“pickedImage”、“showImagePicker”、“imageData”的参数

 @State private var image = UIImage()
    @State var shouldShowImagePicker = false
    var body: some View {
        
        
        NavigationView {
            ScrollView {
                VStack {
                    Image(uiImage: image)
                        .resizable()
                        .cornerRadius(100)
                        .frame(width: 150, height: 150)
                        .clipShape(Circle())
                        .padding()
                        .onTapGesture {
                            shouldShowImagePicker = true }
                    Spacer()
                    
                    Button {
                        shouldShowImagePicker.toggle()
                        
                    } label: {
                        Image(systemName: "person.fill")
                            .font(.system(size: 64))
                            .padding()
                        
                        
                    }
                    Image(uiImage: self.image)
                        .resizable()
                        .cornerRadius(100)
                        .frame(width: 200, height: 200)
                        .background(Color.white)
                        .aspectRatio(contentMode: .fill)
                        .clipShape(Circle())
                }
                .sheet(isPresented: $shouldShowImagePicker) {
                    ImagePicker(sourceType: .photoLibrary, selectedImage: self.$image)
                }

【问题讨论】:

    标签: ios swiftui uiimagepickercontroller


    【解决方案1】:

    @State 属性包装器用于值类型。如果您使用引用类型(即类),则视图不会收到更新通知。

    在您的代码中,您正在使用

    @State private var image = UIImage()
    

    UIImage 是一个班级。

    您应该改用 SwiftUI Image 类型。

    【讨论】:

      猜你喜欢
      • 2023-01-04
      • 2018-10-24
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多