从 Json 中提取图像
Extract image from Json
我创建了这个 Json:
{"daysWeek":[
{
"titleInside":"Monday",
"descriptionInside":"First",
"image":"http://example.com/monday.jpg",
},
{
"titleInside":"Tuesday",
"descriptionInside":"Second",
"image":"http://example.com/Tuesday.jpg",
}]
}
我已经提取了 "titleInside" 和 "descriptionInside" 的信息并将它们放在我的 tableView 中,如下所示:
NSDictionary *days = [self.daysWeek objectAtIndex:indexPath.row];
cell.titleLabel.text = [days objectForKey:@"titleInside"];
cell.descriptionLabel.text = [days objectForKey:@"descriptionInside"]
但我不知道如何将图像放入:
cell.thumbImage.image=?
非常感谢您的帮助。
您将必须下载图像,然后使用生成的数据来配置 UIImage
。如果这是在 table 视图单元格中,您应该异步加载它们。
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: days[@"image"]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.image = [UIImage imageWithData:imageData];
});
});
我建议查看 AFNetworking
及其 UIImageView
类别,以便将来做类似的事情。
我创建了这个 Json:
{"daysWeek":[
{
"titleInside":"Monday",
"descriptionInside":"First",
"image":"http://example.com/monday.jpg",
},
{
"titleInside":"Tuesday",
"descriptionInside":"Second",
"image":"http://example.com/Tuesday.jpg",
}]
}
我已经提取了 "titleInside" 和 "descriptionInside" 的信息并将它们放在我的 tableView 中,如下所示:
NSDictionary *days = [self.daysWeek objectAtIndex:indexPath.row];
cell.titleLabel.text = [days objectForKey:@"titleInside"];
cell.descriptionLabel.text = [days objectForKey:@"descriptionInside"]
但我不知道如何将图像放入:
cell.thumbImage.image=?
非常感谢您的帮助。
您将必须下载图像,然后使用生成的数据来配置 UIImage
。如果这是在 table 视图单元格中,您应该异步加载它们。
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: days[@"image"]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.image = [UIImage imageWithData:imageData];
});
});
我建议查看 AFNetworking
及其 UIImageView
类别,以便将来做类似的事情。