简体   繁体   English

如何显示图片网址中的UIImage

[英]how can I display UIImage from image url

The code below works fine if I give statically but 如果我静态给出以下代码,则可以正常工作,但

imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:@"http://4cing.com/mobile_app/uploads/pageicon/6.jpg"]]]];

Same code - I am passing the image url name dynamically it's not working 相同的代码-我正在动态传递图片网址名称,因此无法正常工作

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

it is working ... i think ur not creating array properly. 它正在工作...我认为您没有正确创建数组。

You need to verify the NSArray contents. 您需要验证NSArray内容。 Log out the objects of the array: 注销数组的对象:

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

Also, a side-note - might want to think about loading that image dynamically. 另外,附带说明-可能需要考虑动态加载该图像。 I wrote a class that makes this pretty painless: 我写了一堂课,让这件事变得很轻松:

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

The answer is geared toward using in TableViews (where lag would really hurt performance), but you can (and I do) use this for any web image loading. 答案是针对在TableViews使用(滞后会真正影响性能),但是您可以(而且我也可以)将其用于任何Web图像加载。 This keeps the UI fluid and is very fast. 这样可以使UI保持流畅并且非常快。

Hi create url in this manner 嗨,以这种方式创建网址

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

i think it will work and will resolve ur problem. 我认为它将起作用,并将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM