我試圖找出一種方法來將圖像通過桌面視圖上的SDWebImage加載到可在全屏中查看圖像的視圖(DetailView)。如何將SDWebImage圖像推送到詳細視圖
我有正確顯示在表格視圖上的URL加載的圖像。但是當我點擊一個,它會繼續到另一個視圖(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.
}
*/
感謝您的答覆加載它,你正在做同樣的方式在你的tableview細胞。我得到一個錯誤,說:'UIImage'類型的值?'沒有成員「sd_setImage」 需要明確的是,我將其粘貼過來:Detailimageview.image =的UIImage(命名爲:SentData1) 這是否意味着錯誤已經SDWebImage沒有被「裝」到DetailViewController? 謝謝! – Miles
對不起,這是一個錯字。固定 – Paulw11
它很好用!非常感謝你,感謝你! – Miles