【问题标题】:Retrieving jpg image from url returns nil. However, retrieving png image from url works fine从 url 检索 jpg 图像返回 nil。但是,从 url 检索 png 图像工作正常
【发布时间】:2017-08-23 16:34:09
【问题描述】:

如果我使用下面的代码,数据将为 nil

dispatch_async(dispatch_get_global_queue(0,0), ^{
    UIImage *img = [[UIImage alloc] init];
    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.manipalgrocer.com/image/catalog/BANNER/sweets.jpg"]];
    img = [UIImage imageWithData: data];
    dispatch_async(dispatch_get_main_queue(), ^{
        menuCell.imgMenu.image = img;
    });
});

但是,如果我使用以下代码,我将在 menuCell 中获取图像

dispatch_async(dispatch_get_global_queue(0,0), ^{
    UIImage *img = [[UIImage alloc] init];
    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"https://www.fooodzapp.com/image/catalog/JUST%20CHILL/Justchill%20(1).png"]];
    img = [UIImage imageWithData: data];
    dispatch_async(dispatch_get_main_queue(), ^{
        menuCell.imgMenu.image = img;
    });
});

如果有人知道如何解决这个问题,请回复。提前致谢

【问题讨论】:

  • 您在尝试加载 http URL 时是否在控制台中看到任何消息?
  • 首先在你的plist中添加ATS,检查图片url是否有多余的空间。然后调用服务

标签: ios objective-c iphone xcode ipad


【解决方案1】:

您的第一张图片是 HTTP 您需要将 App Transport Security 添加到列表中的 yes

 <key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

【讨论】:

  • 非常感谢@Abdelahad Darwish。现在一切正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 2019-08-06
相关资源
最近更新 更多