将图像从Foursquare URL加载到UIImageView

时间:2014-08-04 19:06:04

标签: ios objective-c parsing uiimageview foursquare

我有一个表格视图,我用Foursquare API列出最近的场地。一切都很好,但现在我想添加类别图标。我可以获取该图像的URL,但如何在UIImageView上显示它?

我尝试从Chrome打开该网址但是不可能" 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";
}

1 个答案:

答案 0 :(得分:2)

根据64等可用尺码附加后缀大小。请参阅fourSquare docs中的更多内容。您需要使用sufix添加尺寸

  

将前缀与大小相结合(32,44,64和88可用)

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]];

这是同步的,所以它会阻止main thread使用其他异步选项

相关问题