无法使用URLSession下载图像

时间:2016-09-23 23:39:50

标签: ios nsurlsession swift3 xcode8

我坚持如何正确使用Public Function ReturnXMLResponse(ByVal XML_Method As Variant, _ ByVal XML_Track_URL As Variant, _ ByVal XML_Request As Variant, _ Optional ByVal WaybillNum As String = "", _ Optional ByVal CarrierName As String = "", _ Optional ByVal TotalWaybills As Long = 0, _ Optional ByVal XML_Chunks As Long = 1) As String ' Passed expressions to this function have to be Variant, as some arguments ' may be passed as Null which would result in a type conversion failure. ' If True Then Exit Function ReturnXMLResponse = "Test" ' default if not supported or not tracked by request If UCase(XML_Track_URL) <> "NOT SUPPORTED" And UCase(XML_Track_URL) <> "NOT TRACKED BY REQUEST" Then If (WaybillNum <> "") And (CarrierName <> "") Then TrackingCounter = TrackingCounter + (1 / XML_Chunks) SBText = "Tracking: " & CarrierName & ":" & WaybillNum If TotalWaybills <> 0 Then SBText = SBText & " (" & CLng(TrackingCounter) & "/" & TotalWaybills & ") [" & (TrackingCounter / TotalWaybills) * 100 & "%]" SBText = SBText & "." Application.SysCmd acSysCmdSetStatus, SBText End If Set XMLHTTP = CreateObject("Microsoft.xmlhttp") If (WaybillNum <> "") And (CarrierName <> "") Then SBText = SBText & "." Application.SysCmd acSysCmdSetStatus, SBText End If XMLHTTP.Open XML_Method, XML_Track_URL, False If (WaybillNum <> "") And (CarrierName <> "") Then SBText = SBText & "." Application.SysCmd acSysCmdSetStatus, SBText End If XMLHTTP.Send XML_Request ' okay to send blank string, if not needed If (WaybillNum <> "") And (CarrierName <> "") Then SBText = SBText & "." Application.SysCmd acSysCmdSetStatus, SBText End If ReturnXMLResponse = Cstr(XMLHttp.ResponseText) End If If ReturnXMLResponse = "" Then ReturnXMLResponse = "Nothing" End Function Basically XMLHTTP.Send XML_Request 'XMLHTTP.Send = Sending the XML_Request which is the soap envelope above. It 'then returns the valid XML. 任务将J.S.Bach图像下载到URLSession。 这是UIImage内的代码。不会发生崩溃。程序永远不会到达最里面的if语句,而viewDidLoad仍然是空白的。

UIImageView

1 个答案:

答案 0 :(得分:3)

您正在尝试将网页加载到UIImage实例中。我怀疑您的URLRequest工作正常,但是if let bach行会因为您尝试使用某些HTML创建UIImage而窒息。我意识到维基百科URL在其路径扩展中有“.jpg”,但它仍指向HTML页面。

要解决您的问题,请尝试将网址更改为https://upload.wikimedia.org/wikipedia/commons/6/6a/Johann_Sebastian_Bach.jpg - 这就是您要查找的实际JPEG文件。