如何使用swift 3.0将文本转换为imageview

时间:2017-01-02 16:05:04

标签: ios swift swift3

我想在tableviewcell中的图像视图上显示图像。 保存在文件(本地/服务器)中的图像,其中指向该图像的链接是从JSON文件访问的文本形式。 如何在imageview表单中转换和访问这些链接。 到目前为止,我能够从json文件中获取数据: 其中imageviewcell.swift:

@IBOutlet weak var songimageview: UIImageView!
@IBOutlet weak var imagenameLabel: UILabel!

在viewcontroller中:

func numberOfSections(in tableView: UITableView) -> Int
    {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return arrDict.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {

        let cell: ImageTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "Cell") as! imageTableViewCell
let strTitle : NSString=(arrDict[(indexPath as NSIndexPath).row] as AnyObject).value(forKey: "name") as! NSString

        cell.imagenameLabel.text = strTitle as String

let imagedisplay : NSString=(arrDict[(indexPath as NSIndexPath).row] as AnyObject).value(forKey: "Imagelink") as! NSString

**如何将此imagedisplay转换为imageview,以便在tableview单元格上显示图像。

1 个答案:

答案 0 :(得分:0)

从服务器获取图像(小心同步加载大图像):

 let imageUrl  = "your_link_from_json"
 let url = URL(string: imageUrl)
 let data = try? Data(contentsOf: url!)
 imageView.image = UIImage(data: data!)

来自app main bundle:

 if let filePath = Bundle.main.path(forResource: "imageName", ofType: "png"),
    let image = UIImage(contentsOfFile: filePath) {
        imageView.image = image
 }