2017-01-02 57 views
-4

我想在tableviewcell中的圖像視圖上顯示圖像。 保存在文件(本地/服務器)中的圖像,鏈接到該圖像的文本是以JSON文件訪問的文本形式。 如何轉換並訪問imageview表單中的鏈接。 到目前爲止,我能夠從JSON文件獲取數據: 其中imageviewcell.swift:如何使用swift將文本轉換爲imageview 3.0

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

在視圖 - 控制:

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細胞。

+0

'String'>'URL','URL'>'數據','Data'>'UIImage','UIImage'>'UIImageView' – vadian

+0

這不是一個「告訴我如何做X」的網站。這是一個可以幫助您調試代碼的網站。您的搜索字詞是'UIGraphicsBeginImageContextWithOptions' –

+0

顯示您嘗試過什麼? – Matt

回答

0

獲取從服務器圖像(要小心以syncronously加載大圖像):

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

從應用程序主束:

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