将对象NSData中的图像加载到UIImageView中

问题描述:

我有一个名为http的对象,它从php脚本(它是图像的URL)中检索一个字符串。将对象NSData中的图像加载到UIImageView中

在课堂上,我把它加载到一个NSData成员如下:

profileImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:result]]; 

result是URL字符串,已被选中。

并加载它在我的视图控制器:

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:YES]; 
NSLog(@"appeared"); 
http = [[HTTP alloc] init]; 
[http getProfileImage:[SignInViewController getUsernameString]]; 
NSLog(@"data: %@",http.profileImageData); 
profileImageView.image = [UIImage imageWithData:http.profileImageData]; 
} 

我得到的错误:

2012-03-01 11:03:16.865 Träffa[5574:f803] data: (null) 
2012-03-01 11:03:16.878 Träffa[5574:f803] Error receiving response: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x6b7c840 {NSUnderlyingError=0x6b7c2c0 "bad URL", NSLocalizedDescription=bad URL} 

这是为什么返回null?

编辑:我将问题范围缩小到这一点:

 NSLog(@"%@",result); 
     profileImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:result]]; 
     NSLog(@"%@",profileImageData); 

第一的NSLog与地址返回,上NSData的第二个返回null。我正确地写这篇文章吗?

+0

并获得NSURLErrorDomain代码,你应该检查的第一件事是您选择的网址。 – peko 2012-03-01 16:32:53

想通了。我只好修剪URL字符串,即使它看上去像,因为你是recieving没有数据将被罚款

result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];