2016-10-05 8 views
0

SDWebImageを使用してTableViewに読み込んだイメージを、フルスクリーンで表示できるビュー(DetailView)にプッシュする方法を見つけようとしています。SDWebImageイメージを詳細ビューにプッシュする方法

私は、テーブルビュー上に正しく表示されているURLから読み込まれた画像を持っています。しかし、私が1つをタップすると、そこにUIImageがあるときに空白の別のビュー(DetailView)が表示されます。何らかの理由で、画像がロードされていません。

ありがとうございます!ここで

は、テーブルビューのコードである:ここで

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. 
} 
*/ 

答えて

1

あなたがあなたの目的地VCでUIImage(named:)を使用しています。これは、ネットワークからではなくバンドルからイメージを読み込みます。あなたがロードするとプロパティや変数が慣例

+0

おかげのような何かをすることによってそれをあなたがあなたのテーブルビューのセルにそれをやっているのと同じ方法をロードする必要があります。私は言うエラーを取得しています:タイプの値 'UIImage?'何のメンバーがありません「sd_setImage」 明確にするためには、私はそれを上に貼り付けています:Detailimageview.image = UIImage(命名:SentData1)は は、このエラーがSDWebImageがDetailViewController上に「ロードされ」ていないということですか? ありがとう! – Miles

+0

申し訳ありませんが、それはタイプミスでした。固定 – Paulw11

+0

それは素晴らしい動作します!本当にありがとう、ありがとう! – Miles

0

で小文字で始めるべきだと

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

注:あなたは(またはキャッシュを経由して、すでにフェッチされた場合)、ネットワークからそれを取得するためにsd_setImageを使用する必要があります詳細ビューでは、ロードするイメージのWeb URLを渡していますが、UIImage(named: SentData1)でバンドルからロードしようとしています。

代わりに、あなただけの応答のためのDetailimageview.sd_setImage(with: URL(string: SentData1))

+0

お返事ありがとうございます。私は言うエラーを取得しています:タイプの値 'UIImage?' Detailimageview.image = UIImage(名前:SentData1)これは、SDWebImageがDetailViewControllerに "ロード"されていないことを意味しますか?ありがとうございました! – Miles

+0

私があなたに与えたコードでこのエラーをどのように受け取るのか分かりません。他に何もしていないと確信していますか? – Dima

+0

Paulw11は少し異なるコード行で以下に答えました:Detailimageview.sd_setImage(with:URL(string:self.SentData1))これはうまくいくようです。私は助けに感謝します! – Miles

関連する問題