我从firebase获取图片网址,但是当我尝试在桌面上显示时,我无法看到一个.Code在下面。我正在保存firebase中的图片
的UIImagePickerController()
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "allUsers", for: indexPath) as! allUserCell
cell.textLabel?.text = allUserData[indexPath.row].name
cell.detailTextLabel?.text = allUserData[indexPath.row].email
if let image = allUserData[indexPath.row].image {
let url = NSURL(string: image)
let request = URLRequest(url:url! as URL)
URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
if error != nil{
print(error as Any)
return
}
DispatchQueue.main.async {
if let image = UIImage(data: data!) {
print(image)
cell.imageView?.image = image
}
}
}).resume()
}
return cell
}
}
print(image)行给了我以下 - :
UIImage: 0x60800009b3f0>, {746, 498}
自定义Cell Class。
class allUserCell:UITableViewCell{
override init(style:UITableViewCellStyle,reuseIdentifier:String?) {
super.init(style:.subtitle,reuseIdentifier:reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
答案 0 :(得分:0)
添加扩展程序
extension UIImageView{
func setImageFromURl(stringImageUrl url: String){
if let url = NSURL(string: url) {
if let data = NSData(contentsOfURL: url) {
self.image = UIImage(data: data)
}
}
}
}
然后使用
cell.imageView.setImageFromURl((stringImageUrl: imgURL))
//在参数中提供您的Image url作为String
希望这会对你有所帮助。一切顺利