我有一个本地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
。
随时让我知道,如果我的方法不是我尝试的最佳实践,并给我指示如何实现它。
你IMAGEURL实际上而不是URL。也许这就是为什么 –
@ mninety5:当初始化你的“鸟类”你确定'图像'字符串不是零? – Poles