我有一个表格 View ,其中我使用 Foursquare API 列出了最近的场所。很好,但现在我想添加类别图标。我可以获得该图像的 URL,但如何在 UIImageView 上显示它?
我尝试从 chrome 打开该 URL 但不可能“https://ss1.4sqi.net/img/categories_v2/food/coffeeshop.png”
{
icon = {
prefix = "https://ss1.4sqi.net/img/categories_v2/food/coffeeshop_";
suffix = ".png";
};
id = 4bf58dd8d48988d1e0931735;
name = "Coffee Shop";
pluralName = "Coffee Shops";
primary = 1;
shortName = "Coffee Shop";
}
最佳答案
将大小附加到后缀以响应可用大小,例如 64
。更多信息请引用 fourSquare
docs .你需要添加大小后缀
Combine prefix with a size (32, 44, 64, and 88 are available)
https://ss1.4sqi.net/img/categories_v2/food/coffeeshop_64.png
你可以使用
NSString *imgUrl = [NSString stringWithFormat:@"%@/%@/%@", response[@"icon"][@"prefix"],@"64",response[@"icon"][@"suffix"]]
添加 64
因为它代表大小 64*64
如果你想在 imageview
上设置而不是使用
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: imgUrl]];
UIImageView *yourImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];
这是同步的,所以它会阻止主线程
使用其他异步选项
关于ios - 从 Foursquare URL 加载图像到 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25125766/