Swiftui未能产生表达的诊断。请提交错误报告
在从画廊中选择图像之前,我想从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()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中提到的那样,您正在错误地解开可选图像,您需要使用
if-let
,以使图像具有非nil值。您还将传递
image
实际需要uiimage
。这不是您可以做的。您需要确保您了解使用的类型及其代表的类型。不幸的是,您没有包括
photocaptureView
的代码,因此无法查看您实际需要的代码。这就是为什么要求您提供当您发布问题时。但是,如果它需要
image
或uiimage
,则可以处理它,这与两者都有相似之处。查看“更改”代码中的评论。photocaptureView
使用图像
如果在
photocaptureView
中创建image
,那么您需要进行两个更改。图像>
之前将其解开,photocaptureview
使用uiimage
但是,如果您的
PhotoCaptureView
需要uiimage
,则需要对代码进行三个更改。@state
变量从image
变为uiimage
。uiimage
传递到image 的初始化器中,
我建议您在选项在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 aUIImage
. 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 aUIImage
, there are similarities to both. Look at the comments in the code for the changes.PhotoCaptureView
usesImage
If you are creating an
Image
in thePhotoCaptureView
then there are two changes that you need to make.Image
before you use itPhotoCaptureView
usesUIImage
However, if your
PhotoCaptureView
requires aUIImage
then you need to make three changes to your code.@State
variable from beingImage
into aUIImage
.UIImage
into the initializer forImage(uiImage:)
I would suggest that you read up on Optionals in the Swift documentation, as you seem to have some misconceptions about them.