无法从 URL 获取图像? (NSURLErrorDomain 错误 -1100)
can't get image from URL? (NSURLErrorDomain error -1100)
我正在尝试加载此 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
和那个库似乎没有其他人有问题,所以我认为这仍然是我的设置问题?
您提供的域名有误。
解决方案
删除你手动添加的所有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];
图片下载成功截图
我正在尝试加载此 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
和那个库似乎没有其他人有问题,所以我认为这仍然是我的设置问题?
您提供的域名有误。
解决方案
删除你手动添加的所有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];