[英]how can I display UIImage from image url
如果我静态给出以下代码,则可以正常工作,但
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]]]];
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]]]]];
它正在工作...我认为您没有正确创建数组。
您需要验证NSArray
内容。 注销数组的对象:
NSLog(@"imagename = %@",[imagename description]);
另外,附带说明-可能需要考虑动态加载该图像。 我写了一堂课,让这件事变得很轻松:
https://stackoverflow.com/a/9786513/585320
答案是针对在TableViews
使用(滞后会真正影响性能),但是您可以(而且我也可以)将其用于任何Web图像加载。 这样可以使UI保持流畅并且非常快。
嗨,以这种方式创建网址
[NSURL URLWithString:[[NSString stringWithFormat:@"%@",[imagename objectAtIndex:0]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
我认为它将起作用,并将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.