从url检索jpg图像返回nil。但是,从url检索png图像可以正常工作

时间:2017-08-23 16:34:10

标签: ios objective-c iphone xcode ipad

如果我使用以下代码,数据将为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;
    });
});

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

1 个答案:

答案 0 :(得分:4)

您的第一张图片 HTTP 您需要在列表中将应用传输安全添加到是

enter image description here

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