Swift 2.0没有将图像下载到UI ImageView(Swift 2.0 not downloading images to UI ImageView)

编程入门 行业动态 更新时间:2024-10-26 00:26:00
Swift 2.0没有将图像下载到UI ImageView(Swift 2.0 not downloading images to UI ImageView)

球队,

奇怪的问题。 我冒险进入Swift 2.到目前为止,它非常棒,虽然我不得不做一些代码修复,但它并没有太糟糕。

然后我遇到了这个奇怪的错误(我认为这是一个错误?)

我似乎无法将图像下载到UIImage视图。 我尝试过几种不同的方法。 但似乎都没有效果。 有没有人能够在Swift 2中使用它(从URL填充图像视图)?

这是我最近的尝试:

// // ViewController.swift // uiimage_from_url import UIKit class ViewController: UIViewController { @IBOutlet var image_element: UIImageView! override func viewDidLoad() { super.viewDidLoad() load_image("http://s3.evcdn.com/images/block250/I0-001/016/377/706-0.jpeg_/jazz-cam-graduation-2015-06.jpeg") } func load_image(urlString:String) { let imgURL: NSURL = NSURL(string: urlString)! let request: NSURLRequest = NSURLRequest(URL: imgURL) let response:NSURLResponse = NSURLResponse() let data:NSData = NSData() let error:NSError NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{ response, data, error in /* Your code */ }) self.image_element.image = UIImage(data: data) } }

Team,

Weird problem. I took the plunge into Swift 2. So far it's pretty great and while I've had to do some code fixup, it hasn't been too bad.

Then I hit this weird bug (I think it's a bug?)

I can't seem to download images to a UIImage View. I've tried a few different approaches. But none seem to work. Has anyone been able to get this to work (populating image views from a URL) in Swift 2?

Here's my latest try:

// // ViewController.swift // uiimage_from_url import UIKit class ViewController: UIViewController { @IBOutlet var image_element: UIImageView! override func viewDidLoad() { super.viewDidLoad() load_image("http://s3.evcdn.com/images/block250/I0-001/016/377/706-0.jpeg_/jazz-cam-graduation-2015-06.jpeg") } func load_image(urlString:String) { let imgURL: NSURL = NSURL(string: urlString)! let request: NSURLRequest = NSURLRequest(URL: imgURL) let response:NSURLResponse = NSURLResponse() let data:NSData = NSData() let error:NSError NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{ response, data, error in /* Your code */ }) self.image_element.image = UIImage(data: data) } }

最满意答案

因此,在对这个问题进行投票后,我认为我将不得不自己回答这个问题。 谢谢选民:(

显然iOS 9和我猜测OSX 10.11需要TLSv1.2 SSL用于您计划从中请求数据的所有主机。 除非....您在应用程序的Info.plist文件中指定了例外域。

简单/廉价的修复方法是做这样的事情:

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

我相信有更好/更清洁的方式。 但这将重新启用从任何​​网站下载任何图像。

So after being downvoted for the question, I assumed I was going to have to answer it myself. Thanks down voters :(

Apparently iOS 9 and I'm guessing OSX 10.11 requires TLSv1.2 SSL for all hosts that you plan to request data from. Unless.... you specify exception domains in your application's Info.plist file.

The easy/cheap fix is to do something like this:

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

I'm sure there's a better/cleaner way. But this will re-enable downloading any image from any website.

更多推荐

本文发布于:2023-07-17 07:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   Swift   UI   images   downloading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!