,但是,如果图像负载失败,那么我会遇到一个常见错误,而不是关于为什么URL请求失败的详细错误消息(例如:没有访问URL或其他原因的权限)。

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

可启用代码:

import SwiftUI class Foo { var title: String var url: String var image: Image? init(title: String, url: String, image: Image? = nil) { self.title = title self.url = url self.image = image } } struct ContentViewA: View { @State var showSheetA: Bool = false var body: some View { VStack { Text("This is main view") } .onAppear{ showSheetA = true } .sheet(isPresented: $showSheetA) { SheetViewA() } } } struct SheetViewA: View { @State var data = [ Foo(title: "Image E", url: "https://t3.ftcdn.net/jpg/10/08/34/50/360_F_1008345045_VOe4ziz83vb6d3E3X6KI00qHyLd32D4l.jpg123", image: nil) ] @State var panelDetent: PresentationDetent = .medium var body: some View { NavigationStack { VStack { ScrollView(.horizontal, showsIndicators: false) { HStack(alignment: .center, spacing: 10) { ForEach(data, id: \.title) { item in if let urlObject = URL(string: item.url) { AsyncImage(url: urlObject, scale: 1.0, transaction: Transaction(animation: .spring(response: 0.5, dampingFraction: 0.65, blendDuration: 0.025)), content: { renderPhoto(phase: $0, item: item) }) } else { /// Note: Shows placeholder view EmptyView() } } } .background(Color.gray.opacity(0.2)) .padding(.leading, 0) .padding(.trailing, 16) .frame(maxWidth: .infinity, minHeight: 65, maxHeight: 65, alignment: .topLeading) } } .padding([.top, .bottom], 150.0) .presentationDetents([.medium, .large], selection: $panelDetent) } } @ViewBuilder private func renderPhoto(phase: AsyncImagePhase, item: Foo) -> some View { switch phase { case .success(let image): let _ = print("Success state") case .failure(let error): let _ = print("%%% detailed error is - \(error.localizedDescription) %%%%") case .empty: let _ = print("Empty state") @unknown default: EmptyView() } } }

有一种方法可以使用asycnimage获取详细的错误消息?还是我必须使用旧的urlsession请求方法获取图像?我需要在UI上显示详细的错误消息。
    

我是我的测试代码,它打印了errorerror.localizedDescription

, 向用户提供很少的信息,只是
LoadingError
。 包括测试是使用

URLSession

来获取图像的测试(不存在)。 您的错误似乎为您提供了更多信息(找不到文件), 但这仅仅是因为服务器向您发送此信息。
ios swift swiftui
1个回答
0
投票


最新问题
© www.soinside.com 2019 - 2024. All rights reserved.