使用initWithData加载服务器映像不起作用

时间:2011-12-23 10:31:19

标签: iphone objective-c ios uiimageview uiimage

我正在使用以下代码在UIImageView中显示来自服务器的图像。

NSURL *url = [NSURL URLWithString:@"http://www.gettyicons.com/free-icons/125/miscellaneous/png/256/apple_256.png"];

NSData *imageData =[[NSData alloc] initWithContentsOfURL:url];
imgLargePicture.image = [[UIImage alloc] initWithData:imageData];

我可以看到Saffari中的图片,但图片未在iPhone应用程序中呈现。我在网上搜索了很多,甚至在stackoverflow中,到处都是相同的代码来显示来自服务器的图像。但在我的情况下它不起作用。

任何人都可以帮我这个吗?

3 个答案:

答案 0 :(得分:0)

刚刚在我的应用中尝试了该代码并且有效。你没有添加“imgLargePicture”(我假设它是一个UIImageView)作为你的UIViewController的视图子视图或没有给它一个合适的框架。 在-viewDidLoad:中尝试此操作,看看它是否有效

UIImageView *imgLargePicture = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:imgLargePicture];
NSURL *url = [NSURL URLWithString:@"http://www.gettyicons.com/free-icons/125/miscellaneous/png/256/apple_256.png"];

NSData *imageData =[[NSData alloc] initWithContentsOfURL:url];
imgLargePicture.image = [[UIImage alloc] initWithData:imageData];

答案 1 :(得分:0)

我使用相同的代码并获取图像,因此请检查NSData并确保imageView IBOutlet应正确连接。相同的代码在我身边运行正常,我已经检查了xcode。

答案 2 :(得分:0)

由于组织中的代理服务器设置,这种情况正在发生。它要求我提供用户名和密码通过代理。所以这是通过实现AuthenticationChallenge方法来解决的,我传递了凭据。