iOS Objective-c:如何从URL设置和查看图像
晚上,我正在尝试使用从web下载的图像设置ImageView.image(本例中为IBOutlet) 我已经在堆栈溢出中讨论了很多关于这个的问题 我发现了一个常见的答案:iOS Objective-c:如何从URL设置和查看图像,ios,objective-c,url,uiimageview,Ios,Objective C,Url,Uiimageview,晚上,我正在尝试使用从web下载的图像设置ImageView.image(本例中为IBOutlet) 我已经在堆栈溢出中讨论了很多关于这个的问题 我发现了一个常见的答案: NSURL *url = [NSURL URLWithString:@"http://www.fnordware.com/superpng/pnggrad16rgb.png"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSLog(@"imageData:
NSURL *url = [NSURL URLWithString:@"http://www.fnordware.com/superpng/pnggrad16rgb.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"imageData: %@", data);
UIImage *image = [UIImage imageWithData:data];
NSLog(@"image: %@", image);
self.imageView.image = image;
但当然在我的情况下不起作用。我的日志总是空的
您能告诉我问题出在哪里吗?我相信您正在尝试在iOS 8+上运行它 请将此添加到您的info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
一切都应该很好:)
为什么??
iOS 8以后的http是不允许的。您必须使用https或将异常添加到您的信息中。plist:)我相信您正在尝试在iOS 8+上运行它 请将此添加到您的info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
一切都应该很好:)
为什么??
iOS 8以后的http是不允许的。您必须使用https或将异常添加到您的信息中。plist:)您应该使用库。它还可以缓存url中的图像。并且使用非常简单
[yourImageView sd_setImageWithURL:[NSURL URLWithString:@"image-url"] placeholderImage:[UIImage imageNamed:@"photo.jpg"]]
如果图像尚未加载,将显示占位符图像。您应该使用库。它还可以缓存url中的图像。并且使用非常简单
[yourImageView sd_setImageWithURL:[NSURL URLWithString:@"image-url"] placeholderImage:[UIImage imageNamed:@"photo.jpg"]]
如果图像尚未加载,则会显示占位符图像。此URL为HTTP类型,因此您需要在plist文件中设置NSAppTransportSecurity 因为这个过程是
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
这可能对你有帮助。如果对此有任何问题,请告诉我。此URL是HTTP类型,因此您需要在plist文件中设置NSAppTransportSecurity 因为这个过程是
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
这可能对你有帮助。如果对此有任何问题,请告诉我。您在这里得到的结果是什么
url
”。正确的。您是否有任何关于传输安全的日志,除了您的问题已解决或仍然存在此问题有关传输安全您在这里得到了什么结果url
“”。正确的。您是否有任何关于传输安全的日志,除了您的问题已解决或仍然存在此问题是关于传输安全的问题这就是问题所在!:)@米奥茨:谢谢,伙计:)这就是问题所在!:)@米奥兹:谢谢,伙计:)为什么你又贴了同样的答案为什么你又贴了同样的答案