2010-03-24 35 views
21

我的应用程序必须从http服务器加载图像并将其显示到UIImageView中
我该怎么做?
我尝试这样做:
Xcode iPhone编程:从URL加载一个JPG到UIImageView

NSString *temp = [NSString alloc]; 
[temp stringwithString:@"http://192.168.1.2x0/pic/LC.jpg"] 
temp=[(NSString *)CFURLCreateStringByAddingPercentEscapes(
    nil, 
    (CFStringRef)temp,      
    NULL, 
    NULL, 
    kCFStringEncodingUTF8) 
autorelease]; 


NSData *dato = [NSData alloc]; 
dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:temp]]; 
pic = [pic initWithImage:[UIImage imageWithData:dato]]; 

这段代码是在视图中的viewDidLoad中不显示任何! 服务器正在工作,因为我可以从中加载xml文件。但我无法显示该图像!
我需要以编程方式加载图像,因为它必须根据传递的参数进行更改! 预先感谢您。 安东尼

+0

: 试试这个,你不需要添加
标签的职位 – 2010-03-24 18:03:16

+0

您的代码看起来很怪异,stringwithString:是一类方法,所以基本上我认为[温度stringwithString:@“HTTP:// 192.168.1.2x0/pic/LC.jpg“]无效。你有没有尝试使用dato = [NSData的dataWithContentsOfURL:[NSURL URLWithString:@“http://192.168.1.2x0/pic/LC.jpg”]]? – zonble 2010-03-24 18:06:10

+0

假设图片是你班上的图像 - 是你想要做的任务吗? (pic = [pic initWithImage ...]) – KevinDTimm 2010-03-24 18:07:34

回答

82

它应该是:

NSURL * imageURL = [NSURL URLWithString:@"http://192.168.1.2x0/pic/LC.jpg"]; 
NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 
UIImage * image = [UIImage imageWithData:imageData]; 

一旦你的image变量,你可以把它变成一个UIImageView通过它的image属性,即:

myImageView.image = image; 
//OR 
[myImageView setImage:image]; 

如果你需要逃跑你可以这样做:

NSString * urlString = [@"http://192.168.1.2x0/pic/LC.jpg" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
NSURL * imageURL = [NSURL URLWithString:urlString]; 
.... 

如果您建立UIImageView编程,你这样做:

UIImageView * myImageView = [[UIImageView alloc] initWithImage:image]; 
[someOtherView addSubview:myImageView]; 
[myImageView release]; 
+0

非常感谢你我不知道UIImageView是如何工作的!它非常简单!非常感谢你! – 2010-03-24 22:24:27

+0

警告,dataWithContentsOfURL:不是异步...你可以阻止主线程在这里吗? – Goles 2012-12-31 18:15:30

+0

我发现从网址加载的图像与使用[self.imageView setImage:[UIImage imageNamed:@“AKL.JPG”]]]在本地加载的图像相比显得模糊不清。 – 2014-02-25 05:15:37

3

试试这个

NSURL *url = [NSURL URLWithString:@"http://192.168.1.2x0/pic/LC.jpg"]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 460.0f)]; 
[subview setImage:[UIImage imageWithData:data]]; 
[cell addSubview:subview]; 
[subview release]; 

一切顺利。

3

这是实际的代码。

UIImageView *myview=[[UIImageView alloc]init]; 

    myview.frame = CGRectMake(0, 0, 320, 480); 

    NSURL *imgURL=[[NSURL alloc]initWithString:@"http://soccerlens.com/files/2011/03/chelsea-1112-home.png"]; 

    NSData *imgdata=[[NSData alloc]initWithContentsOfURL:imgURL]; 

    UIImage *image=[[UIImage alloc]initWithData:imgdata]; 

    myview.image=image; 

    [self.view addSubview:myview]; 
3

您应该在后台加载图像或视图将冻结。 FYI

UIImage *img = [[UIImage alloc] init]; 

dispatch_async(dispatch_get_global_queue(0,0), ^{ 

    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.test.com/test.png"]; 
    img = [UIImage imageWithData: data]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     //PUT THE img INTO THE UIImageView (imgView for example) 
     imgView.image = img; 
    }); 
}); 
相关问题