如何将 SDWebImage 图像推送到详细视图
How to push SDWebImage images to a detail view
我正在尝试找出一种方法,将通过 SDWebImage 在 TableView 上加载的图像推送到可以全屏查看图像的视图 (DetailView)。
我从 URL 加载的图像正确显示在 table 视图中。但是当我点击一个时,它会转到另一个视图(DetailView),当我在那里有一个 UIImage 时它是空白的。由于某种原因,图片未加载。
谢谢!
这是 TableView 代码:
import UIKit
class TableViewController: UITableViewController {
var imageURLs = [String]()
override func viewDidLoad() {
super.viewDidLoad()
imageURLs = ["https://i.imgur.com/lssFB4s.jpg","https://i.imgur.com/bSfVe7l.jpg","https://i.imgur.com/vRhhNFj.jpg"]
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailViewController
if let indexpath = self.tableView.indexPathForSelectedRow {
let Imageview = imageURLs[indexpath.row] as String
VC.SentData1 = Imageview
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let cell2: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell
let imagename = UIImage(named: imageURLs[indexPath.row])
cell2.cellImage.image = imagename
let imageView = cell?.viewWithTag(1) as! UIImageView
imageView.sd_setImage(with: URL(string: imageURLs[indexPath.row]))
return cell!
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageURLs.count
}
这里是 DetailView 代码:
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var Detailimageview: UIImageView!
var SentData1:String!
override func viewDidLoad() {
super.viewDidLoad()
Detailimageview.image = UIImage(named: SentData1)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
您正在目的地 VC 中使用 UIImage(named:)
。这将尝试从您的包中加载图像,而不是从网络中加载图像。您应该使用 sd_setImage
从网络获取它(或者如果已经获取则通过缓存):
Detailimageview.sd_setImage(with: URL(string:self.SentData1))
请注意,属性和变量按照惯例应以小写字母开头
当您加载详细视图时,您正在为要加载的图像传递网络 URL,然后尝试使用 UIImage(named: SentData1)
.
从捆绑包中加载它
相反,您应该像在 tableview 单元格中那样加载它,方法是 Detailimageview.sd_setImage(with: URL(string: SentData1))
我正在尝试找出一种方法,将通过 SDWebImage 在 TableView 上加载的图像推送到可以全屏查看图像的视图 (DetailView)。
我从 URL 加载的图像正确显示在 table 视图中。但是当我点击一个时,它会转到另一个视图(DetailView),当我在那里有一个 UIImage 时它是空白的。由于某种原因,图片未加载。
谢谢!
这是 TableView 代码:
import UIKit
class TableViewController: UITableViewController {
var imageURLs = [String]()
override func viewDidLoad() {
super.viewDidLoad()
imageURLs = ["https://i.imgur.com/lssFB4s.jpg","https://i.imgur.com/bSfVe7l.jpg","https://i.imgur.com/vRhhNFj.jpg"]
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailViewController
if let indexpath = self.tableView.indexPathForSelectedRow {
let Imageview = imageURLs[indexpath.row] as String
VC.SentData1 = Imageview
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let cell2: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell
let imagename = UIImage(named: imageURLs[indexPath.row])
cell2.cellImage.image = imagename
let imageView = cell?.viewWithTag(1) as! UIImageView
imageView.sd_setImage(with: URL(string: imageURLs[indexPath.row]))
return cell!
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageURLs.count
}
这里是 DetailView 代码:
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var Detailimageview: UIImageView!
var SentData1:String!
override func viewDidLoad() {
super.viewDidLoad()
Detailimageview.image = UIImage(named: SentData1)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
您正在目的地 VC 中使用 UIImage(named:)
。这将尝试从您的包中加载图像,而不是从网络中加载图像。您应该使用 sd_setImage
从网络获取它(或者如果已经获取则通过缓存):
Detailimageview.sd_setImage(with: URL(string:self.SentData1))
请注意,属性和变量按照惯例应以小写字母开头
当您加载详细视图时,您正在为要加载的图像传递网络 URL,然后尝试使用 UIImage(named: SentData1)
.
相反,您应该像在 tableview 单元格中那样加载它,方法是 Detailimageview.sd_setImage(with: URL(string: SentData1))