【问题标题】:Loading image from object NSData into UIImageView将对象 NSData 中的图像加载到 UIImageView
【发布时间】:2012-03-01 16:07:34
【问题描述】:

我有一个名为 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。我写得对吗?

【问题讨论】:

  • 因为你没有收到任何数据并得到一个 NSURLErrorDomain 代码,你首先应该检查的是你的 url。

标签: objective-c uiimageview nsdata


【解决方案1】:

想通了。我不得不修剪 URL 字符串,即使它看起来没问题

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多