2017-10-10 25 views
1

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

这里是我的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 

    } 

我使用翠鸟缓存图像。试图通过缓存图像时,我得到一个nil

随时让我知道,如果我的方法不是我尝试的最佳实践,并给我指示如何实现它。

+1

你IMAGEURL实际上而不是URL。也许这就是为什么 –

+0

@ mninety5:当初始化你的“鸟类”你确定'图像'字符串不是零? – Poles

回答

1

与意见建议下面我意识到,我是不应该试图让IMAGEURL,因为它是一个本地文件,而不是只返回一个图片对象与指定的文件名。

这是我如何实现它:

override func prepareForReuse() { 
    super.prepareForReuse() 

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

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

你什么时候在你的单元格上设置属性 - 当单元格被重用时,看起来像这可能是零?我建议将你的代码从prepareForReuse()移到你自己的方法,并从cellForRowAt:调用它。喜欢的东西:

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

这很有道理。我刚刚完成了'birdImage.image = UIImage(名为:bird.image!)',它也可以。 – mninety5