Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6天前关闭。
                                                                                            
                
        
我正在尝试将个人资料图片上传到用户个人资料,但个人资料图片未显示在个人资料页面上,没有弹出错误,因此如何重组此代码以解决此问题?

 if let ProfileImageUrl = dictionary?["photo"] as? String {
                     let url = URL(string: ProfileImageUrl)
                    URLSession.shared.dataTask(with: url!, completionHandler: { ( data, response, Error ) in
                        if Error != nil {
                            print(Error!)
                        return
            }
                        DispatchQueue.main.async {
                            self.ProfileImage.image = UIImage(data: data!)
                        }

最佳答案

尾随闭包中的“错误”应为“错误”。这只是命名约定的问题,不一定会导致代码失败。

找到了问题,您在数据任务上丢失了“ .resume()”。以下代码有效:

if let imageUrl = urlString{
        let url = URL(string: imageUrl)
        URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if let error = error{
                print("Error : \(error)")
                return
            }
            DispatchQueue.main.async {
                self.image.image = UIImage(data: data!)
            }
        }.resume()

    }

关于ios - 如何上传用户个人资料图片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59816638/

10-09 06:58