从服务器加载图像到手机中的 UIImageView 上

发布于 2024-10-05 05:27:37 字数 791 浏览 1 评论 0原文

我在将远程图像加载到 UIImageVIew 时遇到问题... 它只是不显示图像,可能是我遗漏了一些东西...

我也使用此处描述的但具有相同的结果 如何从远程服务器加载图像iphone 中的 UIImageView?

有人可以帮助我吗?

这是我正在使用的代码

我从 xml 获取数据,并且在图像元素上我有完整路径

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

使用此代码,图像可以正确显示

[[detailViewController detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://localhost/promos/preview/1.jpeg"]] ]];

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

[[detailViewController detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://localhost/promos/preview/1.jpeg"]] ]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执妄 2024-10-12 05:27:37

由于底部示例按预期工作,并且除了 url 字符串之外,该示例看起来相同,因此问题应该是获取 url 字符串。问题还在于代码看起来相同但很难看清。我会将代码重构为:

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

然后您可以正确调试该语句,并确保在每个步骤后得到您所期望的结果。

更新

要删除换行符和制表符,您可以:

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

如果 urlObject 已经是一个字符串(它应该是),那么您可以执行以下操作:

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

这会使它更干净一些。

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.

揽月 2024-10-12 05:27:37
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://...."]]] retain];
if (img != nil) 
{ 
    [imageViewName setImage:img];
    [img release]; 
}
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://...."]]] retain];
if (img != nil) 
{ 
    [imageViewName setImage:img];
    [img release]; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文