如何从图片网址显示UIImage

时间:2013-03-20 09:08:19

标签: iphone ios

如果我静态地提供

,下面的代码可以正常工作
imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:@"http://4cing.com/mobile_app/uploads/pageicon/6.jpg"]]]];

相同的代码 - 我正在动态传递图片网址名称

imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:@"%@",[imagename objectAtIndex:0]]]];

3 个答案:

答案 0 :(得分:0)

NSMutableArray *imagename = [[NSMutableArray alloc] initWithObjects:@"http://4cing.com/mobile_app/uploads/pageicon/6.jpg", nil];
UIImageView *imgDescView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 200, 200)];

imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                                            [NSURL URLWithString:[NSString stringWithFormat:@"%@",[imagename objectAtIndex:0]]]]];

它正在工作......我想你不能正确创建数组。

答案 1 :(得分:0)

您需要验证NSArray内容。注销数组的对象:

NSLog(@"imagename = %@",[imagename description]);

另外,附注 - 可能想要考虑动态加载该图像。我写了一堂让这很无痛的课:

https://stackoverflow.com/a/9786513/585320

答案是面向在TableViews中使用(滞后会真正影响性能),但是你可以(而且我)将它用于任何网络图像加载。这使UI保持流畅并且非常快。

答案 2 :(得分:0)

你好以这种方式创建网址

[NSURL URLWithString:[[NSString stringWithFormat:@"%@",[imagename objectAtIndex:0]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]

我认为它会起作用,并会解决你的问题。

相关问题