将网址转换为图片

时间:2018-03-20 15:46:57

标签: ios xcode image url download

仍在学习编程并有疑问。

我试图从网址下载图片并将其放入单元格中,我已经成功地使用了文本而不是图像。

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier:"searchCell", for: indexPath)
    as! CustomTableViewCell
    cell.titleField?.text = posts[indexPath.row].caption
    cell.descriptionField?.text = posts[indexPath.row].description
    cell.tagsField?.text = posts[indexPath.row].tags
    let photoUrl = posts[indexPath.row].photoUrl
    cell.SearchImage.image = ...

// url存储在photoUrl

    return cell
}

}

3 个答案:

答案 0 :(得分:0)

我建议您使用此third party library

翠鸟为你处理一切。

  

一个轻量级的纯Swift库,用于从网上下载和缓存图像。

如果您不知道如何安装它,请查看Cocoapods,这非常简单。

无论如何,这是使用Kingfisher从您的URL设置图像所需的唯一代码行:

cell.SearchImage.kf.setImage(with: photoUrl)

顺便说一下,你应该遵循这里描述的Swift惯例,以使你的代码更易于阅读和普及"。

看看:Naming Convention

答案 1 :(得分:0)

使用吊舱轻松实现

使用SDWebImage

然后像这样使用它:

import SDWebImage

cell.SearchImage.sd_setImage(with: photoUrl, placeholderImage: nil)

答案 2 :(得分:0)

开启row3 = tweet_table.loc[3, 'tweet'] row3 '#model i love u take with u all the time in ur\xc3\xb0\xc2\x9f\xc2\x93\xc2\xb1!!! \xc3\xb0\xc2\x9f\xc2\x98\xc2\x99\xc3\xb0\xc2\x9f\xc2\x98\xc2\x8e\xc3\xb0\xc2\x9f\xc2\x91\xc2\x84\xc3\xb0\xc2\x9f\xc2\x91\xc2\x85\xc3\xb0\xc2\x9f\xc2\x92\xc2\xa6\xc3\xb0\xc2\x9f\xc2\x92\xc2\xa6\xc3\xb0\xc2\x9f\xc2\x92\xc2\xa6' print(row3) #model i love u take with u all the time in urð±!!! ððððð¦ð¦ð¦ len(row3) 116 type(row3) str row3_u = row3.decode('utf-8',errors="replace") row3_u u'#model i love u take with u all the time in ur\xf0\x9f\x93\xb1!!! \xf0\x9f\x98\x99\xf0\x9f\x98\x8e\xf0\x9f\x91\x84\xf0\x9f\x91\x85\xf0\x9f\x92\xa6\xf0\x9f\x92\xa6\xf0\x9f\x92\xa6' len(row3_u) 84 print(row3_u) #model i love u take with u all the time in urð±!!! ððððð¦ð¦ð¦ 您可以使用swift3https://cocoapods.org/)。

第1步:

使用pod整合。

Alamofire

第2步:

pod 'Alamofire', '~> 4.7'

pod 'AlamofireImage', '~> 3.3'

第3步:

import AlamofireImage

import Alamofire

或者你可以试试这个 -

Alamofire.request("https:// YOUR URL").responseImage { response in

if let image = response.result.value {
    print("image downloaded: \(image)")
self.myImageview.image = image
}
}
相关问题