0

我正在尝试UIImage通过 URL 初始化。这是我写的代码:

NSString * path = [NSString stringWithFormat:@"%@",[[_tours objectAtIndex:indexPath.row]objectForKey:@"map_url"]];
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc]initWithData:data];
    [[cell tourPreviewImage]setImage:img];
    ....

一个示例map_url如下所示:

https://maps.google.com/maps/api/staticmap?size=200x200&maptype=roadmap&markers=size:big|color:green|72.3601030185,41.4626763775&markers=size:big|color:red|45.4563468516,94.4774796973&sensor=false

似乎NSURL对象无法处理 | 管道字符以正确的方式,因为我没有得到任何图像数据。如果我从 url 中删除由管道分隔的参数,一切正常。

任何想法如何解决这个问题?

4

1 回答 1

0

好的,我找到了解决方案。编码是出了什么问题。

NSString * path = [NSString stringWithFormat:@"%@",[[_tours objectAtIndex:indexPath.row]objectForKey:@"map_url"]];
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
[[cell tourPreviewImage]setImage:img];
于 2012-11-05T10:11:40.007 回答