开发者

Load image from server on a UIImageView in phone

I'm having a problem loading a remote image into a UIImageVIew... It just doesn't show the image, may be i'm missing something...

I also use the described here but with the same results How to load image from remote server on the UIImageView in iphone?

Can someone help me?

This is the code i'm using

Im getting the data from a xml and on the image element I have the full path

[[detailViewController detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat:@"%@", [[promoList objectAtIndex: promoIndex] objectForKey: @"image"]] ]]] ];

With this code the image are displayed correctly

[[detailViewCon开发者_如何学Ctroller detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://localhost/promos/preview/1.jpeg"]] ]];


Since the bottom example works as expected and besides the url string the example looks the same the problem should be with getting the url string. The problem is also that the code looks the same but it is quite hard to see. I would refactor the code to something like:

NSObject *urlObject = [[promoList objectAtIndex:promoIndex] objectForKey:@"image"];
NSString *urlString = [NSString stringWithFormat:@"%@", urlObject];
NSURL *url = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];

[[detailViewController detailImage] setImage:image];

Then you can debug the statement properly and make sure that you get what you expect after each step.

Update

To remove newline and tabs you can:

NSString *urlString = [NSString stringWithFormat:@"%@", urlObject];
urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

If urlObject already is a string, which it should be, then you can do:

NSString *urlString = [urlObject stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

which would make it a bit cleaner.


UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://...."]]] retain];
if (img != nil) 
{ 
    [imageViewName setImage:img];
    [img release]; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜