Swiftui未能产生表达的诊断。请提交错误报告

发布于 2025-02-13 08:17:41 字数 1733 浏览 0 评论 0 原文

在从画廊中选择图像之前,我想从sf-symbole(“ photo.fill)中拥有一个初始图像。但是不幸的是,它向我显示了一个错误。

未能产生表达诊断;请提交错误 报告( https://swift.org/contributing/#reporting-bugs )并包括 项目

代码:

import SwiftUI

struct ContentView: View {

@State private var showImagePicker : Bool = false
@State private var image : Image? = nil

var body: some View {
    
    NavigationView{
        VStack {
            
           Spacer()
            
            if image? {
                Image(uiImage: image?)
                    .resizable()
                    .scaledToFit()
                    .frame(minWidth: 0, maxWidth: .infinity)
                
            } else {
            Image(systemName: "photo.fill")
                .resizable()
                .scaledToFit()
                .opacity(0.6)
                .frame(minWidth: 0, maxWidth: .infinity)
                .padding(.horizontal)
            }
            Spacer()
            
            Button("Öffne Galerie"){
                self.showImagePicker = true
            }.padding()
                .foregroundColor(Color.white)
                .background(Color.blue)
                .cornerRadius(10)
        }
        .sheet(isPresented: self.$showImagePicker) {
            PhotoCaptureView(showImagePicker: self.$showImagePicker, image: self.$image)
        }
        .navigationBarTitle(Text("Foto bearbeiten"))
    }
  }
}
 struct ContentView_Previews: PreviewProvider {
 static var previews: some View {
    ContentView()
  }
}

“代码作为图像”

Before I select an image from the gallery, I want to have an initial image from SF-Symbole ("photo.fill). But unfortunately it shows me an error.

Failed to produce diagnostic for expression; please submit a bug
report (https://swift.org/contributing/#reporting-bugs) and include
the project

The code:

import SwiftUI

struct ContentView: View {

@State private var showImagePicker : Bool = false
@State private var image : Image? = nil

var body: some View {
    
    NavigationView{
        VStack {
            
           Spacer()
            
            if image? {
                Image(uiImage: image?)
                    .resizable()
                    .scaledToFit()
                    .frame(minWidth: 0, maxWidth: .infinity)
                
            } else {
            Image(systemName: "photo.fill")
                .resizable()
                .scaledToFit()
                .opacity(0.6)
                .frame(minWidth: 0, maxWidth: .infinity)
                .padding(.horizontal)
            }
            Spacer()
            
            Button("Öffne Galerie"){
                self.showImagePicker = true
            }.padding()
                .foregroundColor(Color.white)
                .background(Color.blue)
                .cornerRadius(10)
        }
        .sheet(isPresented: self.$showImagePicker) {
            PhotoCaptureView(showImagePicker: self.$showImagePicker, image: self.$image)
        }
        .navigationBarTitle(Text("Foto bearbeiten"))
    }
  }
}
 struct ContentView_Previews: PreviewProvider {
 static var previews: some View {
    ContentView()
  }
}

The code as an image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自演自醉 2025-02-20 08:17:41

正如我在评论中提到的那样,您正在错误地解开可选图像,您需要使用 if-let ,以使图像具有非nil值。

您还将传递 image 实际需要 uiimage 。这不是您可以做的。您需要确保您了解使用的类型及其代表的类型。

不幸的是,您没有包括 photocaptureView 的代码,因此无法查看您实际需要的代码。这就是为什么要求您提供当您发布问题时。

但是,如果它需要 image uiimage ,则可以处理它,这与两者都有相似之处。查看“更改”代码中的评论。

photocaptureView 使用图像

如果在 photocaptureView 中创建 image ,那么您需要进行两个更改。

  • 您需要在使用图像> 之前将其解开,
  • 因为您拥有SwiftUI映像,只需直接使用它即可。
struct ContentView: View {

    @State private var showImagePicker : Bool = false
    @State private var image : Image? = nil

    var body: some View {

        NavigationView{
            VStack {

                Spacer()

                if let image = image { // here we unwrap the Image
                    image              // as we have a SwiftUI Image we can use it directly.
                        .resizable()
                        .scaledToFit()
                        .frame(minWidth: 0, maxWidth: .infinity)

                } else {
                    Image(systemName: "photo.fill")
                        .resizable()
                        .scaledToFit()
                        .opacity(0.6)
                        .frame(minWidth: 0, maxWidth: .infinity)
                        .padding(.horizontal)
                }
                Spacer()

                Button("Öffne Galerie"){
                    self.showImagePicker = true
                }.padding()
                    .foregroundColor(Color.white)
                    .background(Color.blue)
                    .cornerRadius(10)
            }
            .sheet(isPresented: self.$showImagePicker) {
                PhotoCaptureView(showImagePicker: self.$showImagePicker, image: self.$image)
            }
            .navigationBarTitle(Text("Foto bearbeiten"))
        }
    }
}

photocaptureview 使用 uiimage

但是,如果您的 PhotoCaptureView 需要 uiimage ,则需要对代码进行三个更改。

  • 首先,我们需要将您的 @state 变量从 image 变为 uiimage
  • 然后,我们可以以与上面相同的方式解开图像,
  • 然后将未包装 uiimage 传递到 image 的初始化器中,
struct ContentView: View {

    @State private var showImagePicker : Bool = false
    @State private var image : UIImage? = nil // Here we use UIImage

    var body: some View {

        NavigationView{
            VStack {

                Spacer()

                if let image = image {    // We unwrap the UIImage so that we can use it
                    Image(uiImage: image) // Here we convert the UIImage into a SwiftUI Image
                        .resizable()
                        .scaledToFit()
                        .frame(minWidth: 0, maxWidth: .infinity)

                } else {
                    Image(systemName: "photo.fill")
                        .resizable()
                        .scaledToFit()
                        .opacity(0.6)
                        .frame(minWidth: 0, maxWidth: .infinity)
                        .padding(.horizontal)
                }
                Spacer()

                Button("Öffne Galerie"){
                    self.showImagePicker = true
                }.padding()
                    .foregroundColor(Color.white)
                    .background(Color.blue)
                    .cornerRadius(10)
            }
            .sheet(isPresented: self.$showImagePicker) {
                PhotoCaptureView(showImagePicker: self.$showImagePicker, image: self.$image)
            }
            .navigationBarTitle(Text("Foto bearbeiten"))
        }
    }
}

我建议您在选项在Swift文档中,您似乎对它们有一些误解。

As I mentioned in my comment you are unwrapping the optional image incorrectly, you need to use an if-let so that you have a non-nil value for your image.

You are also passing an Image where you actually require a UIImage. This is not something that you can do. You need to make sure that you understand that types that you using and what they represent.

Unfortunately you did not include the code for your PhotoCaptureView so it is not possible to see what you actually require. This is why you are asked to provide a minimum reproducible example when you post a question on SO.

However, here is how you can handle it if it requires an Image or a UIImage, there are similarities to both. Look at the comments in the code for the changes.

PhotoCaptureView uses Image

If you are creating an Image in the PhotoCaptureView then there are two changes that you need to make.

  • You need to unwrap your Image before you use it
  • As you have a SwiftUI Image you can just use it directly.
struct ContentView: View {

    @State private var showImagePicker : Bool = false
    @State private var image : Image? = nil

    var body: some View {

        NavigationView{
            VStack {

                Spacer()

                if let image = image { // here we unwrap the Image
                    image              // as we have a SwiftUI Image we can use it directly.
                        .resizable()
                        .scaledToFit()
                        .frame(minWidth: 0, maxWidth: .infinity)

                } else {
                    Image(systemName: "photo.fill")
                        .resizable()
                        .scaledToFit()
                        .opacity(0.6)
                        .frame(minWidth: 0, maxWidth: .infinity)
                        .padding(.horizontal)
                }
                Spacer()

                Button("Öffne Galerie"){
                    self.showImagePicker = true
                }.padding()
                    .foregroundColor(Color.white)
                    .background(Color.blue)
                    .cornerRadius(10)
            }
            .sheet(isPresented: self.$showImagePicker) {
                PhotoCaptureView(showImagePicker: self.$showImagePicker, image: self.$image)
            }
            .navigationBarTitle(Text("Foto bearbeiten"))
        }
    }
}

PhotoCaptureView uses UIImage

However, if your PhotoCaptureView requires a UIImage then you need to make three changes to your code.

  • Firstly we would need to change your @State variable from being Image into a UIImage.
  • We can then unwrap the image the same way as above
  • We then pass the unwrapped UIImage into the initializer for Image(uiImage:)
struct ContentView: View {

    @State private var showImagePicker : Bool = false
    @State private var image : UIImage? = nil // Here we use UIImage

    var body: some View {

        NavigationView{
            VStack {

                Spacer()

                if let image = image {    // We unwrap the UIImage so that we can use it
                    Image(uiImage: image) // Here we convert the UIImage into a SwiftUI Image
                        .resizable()
                        .scaledToFit()
                        .frame(minWidth: 0, maxWidth: .infinity)

                } else {
                    Image(systemName: "photo.fill")
                        .resizable()
                        .scaledToFit()
                        .opacity(0.6)
                        .frame(minWidth: 0, maxWidth: .infinity)
                        .padding(.horizontal)
                }
                Spacer()

                Button("Öffne Galerie"){
                    self.showImagePicker = true
                }.padding()
                    .foregroundColor(Color.white)
                    .background(Color.blue)
                    .cornerRadius(10)
            }
            .sheet(isPresented: self.$showImagePicker) {
                PhotoCaptureView(showImagePicker: self.$showImagePicker, image: self.$image)
            }
            .navigationBarTitle(Text("Foto bearbeiten"))
        }
    }
}

I would suggest that you read up on Optionals in the Swift documentation, as you seem to have some misconceptions about them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文