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

时间:2012-03-01 16:07:35

标签: objective-c uiimageview nsdata

我有一个名为http的对象,它从php脚本中检索字符串,该脚本是图像的URL。

在类中,我将它加载到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。我写得好吗?

1 个答案:

答案 0 :(得分:0)

想出来。我不得不修剪URL字符串,即使它看起来很好

result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
相关问题