图像路径未在swift3中转换为url

时间:2017-06-05 12:01:50

标签: ios iphone swift swift3 uiimageview

我有帖子回复我想从$("#description").html("<pre>for CCAET and has received significant international scientific recognition. <br /> \n Dr. Armogan has also been responsible for many surgical firsts in retina and cataract management.</pre>");

下载图片
image_path

更新:

[1]: https://i.stack.imgur.com/KWC S9.png

1 个答案:

答案 0 :(得分:0)

该URL不是本地文件路径URL,也不是符合我浏览器的有效URL。

您在上面提供的URL会在浏览器中返回服务器错误,但不会返回图像。见截图

Screenshot

您需要确保图像可以访问,并且URL实际上首先返回图像响应。然后你需要下载图像。不确定你是否使用任何库,所以我将发布一个没有的例子。

//: Playground - noun: a place where people can play

import UIKit
import XCPlayground
import PlaygroundSupport

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

// random image from images.google.com
let urlString = "https://files.allaboutbirds.net/wp-content/uploads/2015/06/prow-featured-240x135.jpg"

let url = URL(string: urlString)
let session = URLSession.shared

let task = session.dataTask(with: url!) { data, response, error in
    guard error == nil else {
        print("[ERROR] - Failed to download image")
        return
    }

    if let data = data {
        let image = UIImage(data: data)
        DispatchQueue.main.async {
            imageView.image = image
        }
    }
}

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.addSubview(imageView)

task.resume()
PlaygroundPage.current.liveView = view

更新:

Screenshot 2