2017-04-27 4 views
0

現在、ファイヤーベースのストレージから画像を読み取っています。これはうまくいきます。速報でapp iosのキャッシュから画像を読み取る

// Storage.imageCache.object(forKey: post.imageUrl as NSString) 
static func getImage(with url: String, completionHandler: @escaping (UIImage) ->()) 
{ 
    if let image = imageCache.object(forKey: url as NSString) 
    { 
     print("CACHE: Unable to read image from CACHE ") 

     completionHandler(image) 
    } 
    else 
    { 
     let ref = FIRStorage.storage().reference(forURL: url) 
     ref.data(withMaxSize: 2 * 1024 * 1024) 
     { 
      (data, error) in 

      if let error = error 
      { 
       print("STORAGE: Unable to read image from storage \(error)") 
      } 
      else if let data = data 
      { 
       print("STORAGE: Image read from storage") 
       if let image = UIImage(data: data) 
       { 
        // Caches the image 
        Storage.imageCache.setObject(image, forKey: url as NSString) 
        completionHandler(image) 
       } 
      } 
     } 
    } 
} 
} 

をしかし、その動作していない:

私はそれがストレージから読み出されたときにキャッシュから画像を読み込むためのキャッシュを設定しています。それはまったくうまくいかないようですが、私は 'print( "CACHE:CACHEから画像を読み取ることができません")というメッセージは表示されません。 'ストレージ」) '

これはどのように実現できますかご存知ですか?

ありがとうございました!

--- EDIT -

私のように、その後firebaseストレージから表のセルのビューで画像を呼び出す:あなたはたとえばカワセミとキャッシュ画像を実現することができます

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = self.feedTableView.dequeueReusableCell(withIdentifier: "MessageCell")! as UITableViewCell 

    let imageView = cell.viewWithTag(1) as! UIImageView 
    let titleLabel = cell.viewWithTag(2) as! UILabel 
    let linkLabel = cell.viewWithTag(3) as! UILabel 

    titleLabel.text = posts[indexPath.row].title 
    titleLabel.numberOfLines = 0 

    linkLabel.text = posts[indexPath.row].link 
    linkLabel.numberOfLines = 0 



    Storage.getImage(with: posts[indexPath.row].imageUrl){ 
     postPic in 
     imageView.image = postPic 
    } 

    return cell 

} 
+0

を役に立てば幸いプレースホルダ画像や素晴らしいですキャッシュの読み込み:https://github.com/rs/SDWebImage –

答えて

0

。そしてうまくいく。 link

使用方法:ストレージからデータベースアイテムノードへのイメージへのリンクを追加します。このように:

enter image description here

それからちょうどイメージを提示してキャッシュするためにそれを使用。

例:

let imageView = UIImageView(frame: frame) // init with frame for example 
imageView.kf.setImage(with: <urlForYourImageFromFireBase>) //Using kf for caching images 

は、このライブラリは(私もfirebaseでそれらを使用)、インターネットから画像をダウンロードするための素晴らしいですが、あなたが進行、完了ハンドラを持つことができ、それは

関連する問題