Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将firebase图像URL中的图像设置为Swift中的单元格_Ios_Swift_Firebase_Sdwebimage - Fatal编程技术网

Ios 将firebase图像URL中的图像设置为Swift中的单元格

Ios 将firebase图像URL中的图像设置为Swift中的单元格,ios,swift,firebase,sdwebimage,Ios,Swift,Firebase,Sdwebimage,如果ImageView位于同一视图控制器中,我可以使其工作。但是我创建了一个自定义cell.xib和一个模型类。所有数据(文本)似乎都在传输,甚至Firebase数据库图像的URL也在传输,但图像没有改变,显示的是默认的占位符图像。如果我没有设置默认的占位符图像,我会得到一个错误,说它是零。。同样,所有单元格都在填充来自Firebase的文本数据,唯一没有填充的是来自给定URL的图像。调试表明URL确实被传递到“profileImageURL”变量中。下面是一些代码: class Profile

如果ImageView位于同一视图控制器中,我可以使其工作。但是我创建了一个自定义cell.xib和一个模型类。所有数据(文本)似乎都在传输,甚至Firebase数据库图像的URL也在传输,但图像没有改变,显示的是默认的占位符图像。如果我没有设置默认的占位符图像,我会得到一个错误,说它是零。。同样,所有单元格都在填充来自Firebase的文本数据,唯一没有填充的是来自给定URL的图像。调试表明URL确实被传递到“profileImageURL”变量中。下面是一些代码:

class Profile{

var name: String = ""
var age: String = ""
var profileImg : UIImageView! = UIImageView.init(image: "main"))
var description : String = ""


 }
以下是表视图控制器代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellTableViewCell", for: indexPath) as! CustomCellTableViewCell

    let profile = profileArray[indexPath.row]
    // Configure the cell...
    profileTableView.rowHeight = 300
    cell.nameLbl.text = profile.name
    cell.descriptionLbl.text = profile.description
    cell.profileImage.image = UIImage(named: "main")

    return cell
}

func retrievePosts(){


    let dataRef = Database.database().reference()

    let postsDB = dataRef.child("Messages")

    postsDB.observe(.childAdded) { (snapshot) in
    let value = snapshot.value as! Dictionary<String,String>

    let text = value["postText"]!
    let profileImgURL = value["postImage"]!
    let userID = value["sender"]!

    let url = NSURL(string:"\(profileImgURL)")

    let profile = Profile()
    profile.description = text
    profile.name = name as! String
    profile.profileImg.sd_setImage(with: URL(string:profileImgURL), placeholderImage: nil)  


    self.profileTableView.reloadData()
    self.profileArray.insert(profile, at: 0)

        })

}

here is the firebase data structure:
        - Messages
          -L4IkuSxWDnsiJvTKoo0
              -postImage: "https://firebasestorage.googleapis.co......"
              -postText: "Lorem ipsum dolor sit er elit lamet, consecteta..."
              -sender: "C19ghii6OVPNnzJNYPvVJeKuoi73"
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“CustomCellTableViewCell”,for:indexPath)作为!CustomCellTableViewCell
让profile=profileArray[indexPath.row]
//配置单元格。。。
profileTableView.rowHeight=300
cell.nameLbl.text=profile.name
cell.descriptionBL.text=profile.description
cell.profileImage.image=UIImage(名为:“main”)
返回单元
}
func retrievePosts(){
让dataRef=Database.Database().reference()
让postsDB=dataRef.child(“消息”)
postsDB.observe(.childAdded){(快照)在
设value=snapshot.value为!字典
让文本=值[“postText”]!
让profileImgURL=value[“positmage”]!
让userID=value[“发件人”]!
让url=NSURL(字符串:“\(profileImgURL)”)
设profile=profile()
profile.description=文本
profile.name=名称为!字符串
profile.profileImg.sd_setImage(带:URL(字符串:profileImgURL),占位符image:nil)
self.profileTableView.reloadData()
self.profileArray.insert(配置文件,位于:0)
})
}
以下是firebase数据结构:
-信息
-L4IkuSxWDnsiJvTKoo0
-后映像:“https://firebasestorage.googleapis.co......"
-postText:“Lorem ipsum dolor sit er elit lamet,Concerteta…”
-发送方:“C19GHII6OVPNNZJNYPVJEKUAI73”

这不是真正的解决方案,而是一种替代方案:

使用翠鸟吊舱从URL设置图像

let url = URL(string: "https://example.com/image.jpg")!
imageView.kf.setImage(with: url)

bro,任何单元格的图像都将在cellForRowAt方法中设置。您获取原始数据是因为您是从配置文件而不是硬编码获取数据,而对于图像,您每次都将图像设置为“主”。希望这能有所帮助阿什·夏尔马
是的!非常感谢。应用程序开发新手。就是这样。必须在Profile中创建一个新变量来保存url,然后在cellForRowAt中使用该变量url设置Imageview。很可能有一种更简单的方法,但对我来说很有效!非常感谢吉列尔莫·格雷科(Guillermo Greco)

谢谢你,但那也一样。我也尝试过PinRemoteLibrary,它类似于KF和SDWeb,但它们的功能是相同的。他们工作,但我做错了什么。我想是因为我没有进入当地的房产?我已经看到了许多如何使用它的示例,但没有一个示例说明如何将它应用到另一个类的属性:let profile=profile()。。。。。然后将其称为profile.profileImage.SD_setImage…bro,任何单元格的图像都将在
cellForRowAt
方法中设置。您获取原始数据是因为您是从配置文件而不是硬编码获取数据,而对于图像,您每次都将图像设置为“主”。希望这有帮助。是的!非常感谢。应用程序开发新手。就是这样。必须在Profile中创建一个新变量来保存url,然后在cellForRowAt中使用该变量url设置Imageview。很可能有一种更简单的方法,但对我来说很有效!非常感谢你!