本文介绍了如何上传用户个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将个人资料图片上传到用户个人资料,但个人资料图片未显示在个人资料页面上,没有弹出错误,那么如何重组此代码以解决此问题?
I am trying to upload a profile image to the user profile but the profile image doesn't appear on the profile page there are no errors popping up so how do I restructure this code to fix this issue?
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!) }推荐答案
结尾闭包中的错误"应为错误".这只是命名约定的问题,不一定会导致代码失败.
The "Error" in trailing closure should be "error". It's just a matter of naming convention, doesn't necessarily cause the code to fail.
发现问题,您在数据任务上缺少".resume()".以下代码有效:
Found the problem, you're missing ".resume()" on the data task. The following code works :
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() }更多推荐
如何上传用户个人资料图片
发布评论