使用带有本地JSON数据Swift的图像

时间:2017-10-10 10:48:50

标签: ios json

我有一个本地JSON数据,可以在应用加载时填充我的视图。我现在正在尝试加载本地图像并将它们附加到必要的对象上。

这是我的JSON数据:

{
    "description": "Birds of Antarctica, grouped by family",
    "source": "https://en.wikipedia.org/wiki/List_of_birds_of_Antarctica",
    "birds": [
              {
              "bio": "Albatrosses are highly efficient in the air, using dynamic soaring and slope soaring to cover great distances with little exertion. They feed on squid, fish and krill by either scavenging, surface seizing or diving. Albatrosses are colonial, nesting for the most part on remote oceanic islands, often with several species nesting together.",
              "family": "Albatrosses",
              "imageURL": "albatross.jpg",
              "members": [
                          "Wandering albatross",
                          "Grey-headed albatross",
                          "Black-browed albatross",
                          "Sooty albatross",
                          "Light-mantled albatross"
                          ]
              },
              {
              "bio": "Terns and shags are medium-to-large birds, with body weight in the range of 0.35–5 kilograms (0.77–11.02 lb) and wing span of 45–100 centimetres (18–39 in). The majority of species have dark feathers. The bill is long, thin and hooked. Their feet have webbing between all four toes. All species are fish-eaters, catching the prey by diving from the surface.",
              "family": "Terns",
              "imageURL": "terns.jpg",
              "members": [
                          "Arctic tern",
                          "Antarctic tern"
                          ]
              }
              ]
}

我可以使用此处提出的建议获得家人,生物和会员:Correctly parsing through nested JSON using data model for multiple reuse. Swift

在用于填充表格的BirdCell类中,我有这个:

class BirdCell: UITableViewCell {

    var bird: Bird!
    var imageUrl: String!
    @IBOutlet weak var nameLbl: UILabel!
    @IBOutlet weak var birdImage: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func prepareForReuse() {
        super.prepareForReuse()

        birdImage.image = nil
        imageUrl = bird.imageUrl?.absoluteString

        if let imageUrl = bird.imageUrl {
            let resource = ImageResource(downloadURL: imageUrl)
            birdImage.kf.setImage(with: resource)

            if birdImage.image == nil {

                birdImage.image = UIImage(named: "placeholder")
            } else {

            }
        }
        nameLbl.text = bird.family

    }

我正在使用 Kingfisher 来缓存图片。我试图通过缓存图像时得到nil

如果我的方法不是我尝试的最佳做法,请随时告诉我,并告诉我如何实现它。

2 个答案:

答案 0 :(得分:1)

如何将图像添加到您的项目中?我猜它们在你的Images.xcassets文件中?在这种情况下,只需仔细检查JSON中的名称是否与资产文件中的名称相匹配。如果文件是本地文件,为什么要尝试创建图像URL?

您何时在单元格上设置 bird 属性 - 当单元格被重用时,这看起来可能为零?我建议将代码从prepareForReuse()移到您自己的方法,并从cellForRowAt:调用它。类似的东西:

func updateWithBird(bird: Bird) {
    birdImage.image = UIImage(named: bird.image)
    nameLbl.text = bird.family
}

答案 1 :(得分:1)

根据以下评论中的建议,我意识到我不应该尝试获取imageURL,因为它是一个本地文件,而只是返回一个具有指定文件名的图像对象。

这就是我实现它的方式:

override func prepareForReuse() {
    super.prepareForReuse()

    birdImage.image = UIImage(named: bird.image!)
    nameLbl.text = bird.family
}