无法从 URL 获取图像? (NSURLErrorDomain 错误 -1100)

Posted

技术标签:

【中文标题】无法从 URL 获取图像? (NSURLErrorDomain 错误 -1100)【英文标题】:can't get image from URL? (NSURLErrorDomain error -1100) 【发布时间】:2016-04-19 05:28:50 【问题描述】:

我正在尝试加载此 jpeg:https://i.groupme.com/638x640.jpeg.d4f31c747b534baca03d12db5a2b6193

但我收到错误 The operation couldn’t be completed. (NSURLErrorDomain error -1100.)

从this post,我发现错误的意思是:kCFURLErrorFileDoesNotExist。这很奇怪,因为那里肯定有图像。

来自this post,似乎问题出在https 协议上,但在我实施切换到http 的解决方案后,我得到了错误:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file. ... The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.

在进行必要的 plist 更改后,同样的错误继续弹出:

如何在 ios 中下载此图像?

更新

所以我发现如果我使用此代码,它确实可以使用https 下载:

NSData* theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://i.groupme.com/638x640.jpeg.d4f31c747b534baca03d12db5a2b6193"]];
UIImage *image = [UIImage imageWithData:theData];

但是如果我使用这个库就不行了:https://github.com/rs/SDWebImage

似乎没有其他人对https 和那个库有任何问题,所以我认为这仍然是我的设置问题?

【问题讨论】:

Transport Security has Blocked a cleartext HTTP的可能重复 如果您要关闭它,请解释原因。如果您阅读了整个问题,您就会知道它不是另一个问题的重复 您的 URL 包含 https,因此NSExceptionAllowsInsecureHTTPLoads 没有问题。请给我看代码来帮助你......它必须与https一起工作,不需要去http...... @NiravGadhiya 感谢您的提示,在简化了我的代码以提供示例后,我下载了它。似乎问题只出现在我正在使用的图像加载库中,但他们的示例代码中有一个https url,所以它可能仍然是我的问题github.com/rs/SDWebImage 【参考方案1】:

您提供的域名有误。

解决方案

删除你手动添加的所有plist设置,在plist中添加这个----

 <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>i.groupme.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
                <false/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSThirdPartyExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSRequiresCertificateTransparency</key>
                <false/>
            </dict>
        </dict>
    </dict>

Plist 截图

图片下载代码

 NSURL *url = [[NSURL alloc] initWithString:@"https://i.groupme.com/638x640.jpeg.d4f31c747b534baca03d12db5a2b6193"];
 NSData *data = [[NSData alloc] initWithContentsOfURL:url];
 UIImage *result = [[UIImage alloc] initWithData:data];

图片下载成功截图

【讨论】:

NSIncludesSubdomains 不包括i. 部分吗? 谢谢!我刚刚使用了该代码并且它有效,似乎是我正在使用的库。但是,我仍然对为什么这个库不适合我感到困惑:github.com/rs/SDWebImage。他们的示例代码中有https,所以这对我来说绝对是个问题。

以上是关于无法从 URL 获取图像? (NSURLErrorDomain 错误 -1100)的主要内容,如果未能解决你的问题,请参考以下文章

无法从 URL 获取图像? (NSURLErrorDomain 错误 -1100)

无法从 API 获取图像

使用汉字从URL获取图像

从 https url ios 获取图像

Python:从 URL 获取图像尺寸

Firestore/Firebase 无法从文件夹中获取图像