【问题标题】:How to get image from server using NSUrlConnection [duplicate]如何使用 NSUrlConnection 从服务器获取图像 [重复]
【发布时间】:2013-06-28 12:50:46
【问题描述】:

如何通过 url 发送一个参数从服务器获取图像。

GET HTTPRequest 必须用于发送请求。

【问题讨论】:

  • 谷歌有很多关于这个问题的答案。
  • 你能告诉我一些代码或建议吗
  • @IOS_5555:我已经给你建议了。你看到了吗?
  • 谢谢大家,我自己解决了这个问题
  • 请更正您的拼写

标签: iphone ios objective-c nsurlconnection


【解决方案1】:

您可以从服务器获取带有图像名称的图像的特定路径的图像。

UIImage* serverImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://example.com/image_name.png"]]];

那么你可以在任何你想要的地方使用serverImage

【讨论】:

    【解决方案2】:

    尝试使用这个:

    -(UIImage*)getImageFromURLwithUrl:(NSString*)imgURLStr
    {
        NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imgURLStr]];
        NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];
        UIImage *image = [UIImage imageWithData:imageData];
    
        return image;
    }
    

    【讨论】:

      【解决方案3】:

      SDWebImage 是我用来加载图像而不阻塞 UI 的一个非常有用的项目。 它在 UIImageView 上添加了一个异步类别,使您只需一行代码即可加载图像:

      [myImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                     placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
      

      【讨论】:

      • 是你挡住了 UI。下载完成后您正在更新您的 UI,您应该异步使用它。在主线程上更新你的 UI 是 SDWebImage 的职责。
      • 没错,所以上面的代码并没有阻塞主线程,因为 SDWebImage 是在后台线程上下载,然后在完成后在主线程上更新。
      猜你喜欢
      • 2010-11-20
      • 2017-06-28
      • 2018-05-30
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      相关资源
      最近更新 更多