如何将SDWebImage图像推送到详细视图

时间:2016-10-05 00:02:46

标签: ios swift sdwebimage

我试图找到一种方法将通过TableView上的SDWebImage加载的图像推送到可以全屏查看图像的视图(DetailView)。

我正在从正确显示在表格视图上的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.
}
*/

2 个答案:

答案 0 :(得分:1)

您在目的地VC中使用UIImage(named:)。这将尝试从您的包中加载图像,而不是从网络加载。您应该使用sd_setImage从网络中获取它(如果已经获取,则使用缓存):

 Detailimageview.sd_setImage(with: URL(string:self.SentData1))  

请注意,属性和变量应按惯例以小写字母开头

答案 1 :(得分:0)

加载详细信息视图时,您要传递要加载的图片的网址,然后尝试使用UIImage(named: SentData1)从包中加载它。

相反,您应该像在Detailimageview.sd_setImage(with: URL(string: SentData1))

那样加载它,就像在tableview单元格中一样加载它