【问题标题】:Getting an image fails to create NSURL获取图片无法创建 NSURL
【发布时间】:2013-11-24 05:34:04
【问题描述】:

我正在尝试从 url 链接获取图像。

        NSURL *thmbnailUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:800/fishtune/movie/%@.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]];
        NSLog(@"thmbnail:%@",[NSString stringWithFormat:@"http://%@:800/fishtune/movie/%@.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]);
        NSData *data = [NSData dataWithContentsOfURL:thmbnailUrl];
        NSLog(@"movimgdata:%@",data);
        UIImage *img = [[UIImage alloc]initWithData:data];
        NSLog(@"movimg:%@",data);
        NSData *imgData = UIImagePNGRepresentation(img);

当我记录缩略图时,我得到这个“ipofserverinnetwork/fishtune/movie/Our Logo 480p back.png” 注意:我没有放原ip。

当我记录数据时我得到空值,当然 img 也将为空值。我真的不知道为什么我会为空。因为当我尝试在浏览器中输入网址时,它会显示图像。我在我的应用程序的另一部分使用了相同的代码并且它可以工作,但是当我在其中使用它时它失败了。是因为“Our_Logo_480p”之间的空格吗?

【问题讨论】:

标签: ios nsurl


【解决方案1】:

你可能需要使用

NSString* escapedUrl = [yourstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

【讨论】:

    【解决方案2】:

    是的,这是因为空格,使用

     NSString *myUrl = [NSString stringWithFormat:@"http://%@:800/fishtune/movie/%@.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]];
       NSString *url = [myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 2013-06-26
      • 1970-01-01
      • 2018-12-09
      相关资源
      最近更新 更多