2016-10-11 2 views
1

イメージを1つずつダウンロードし、ダウンロードしたイメージを最初にロードするように設定するにはどうすればよいですか?また、ユーザーが上にある画像をパージしたり消したりしながらスクロールダウンするときに、より多くの画像ダウンロードを処理する方法はありますか?ここでユーザーがcollectionsViewでスクロールダウンするときに画像をロードするには?

がFirebaseから非同期にこれらの画像をダウンロードするための私のコードです:

let databaseRef = FIRDatabase.database().reference() 

      databaseRef.child("palettes").queryOrdered(byChild: "top").queryEqual(toValue: "#000000").observeSingleEvent(of: .value, with: { (snapshot) in 


       if let snapDict = snapshot.value as? [String:AnyObject]{ 

        for each in snapDict as [String:AnyObject]{ 

         let URL = each.value["URL"] as! String 

         if let url = NSURL(string: URL) { 
          if let data = NSData(contentsOf: url as URL){ 
           let image = UIImage(data: data as Data) 
           self.imageArray.append(image!) 
           self.collectionView?.reloadData() 
          } 
         } 


        } 
       } 
      }) 

そして、ここではcollectionViewを取り込むための私のコードです:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
     let textLabel = cell.viewWithTag(2) 

     let ootdImage = cell.viewWithTag(4) as! UIImageView 

     ootdImage.image = imageArray[indexPath.row] 


     textLabel?.backgroundColor = averageColor 

     return cell 
    } 

編集:今、私のJSONツリーのみが含まれて3つのイメージがダウンロードされます。しかし、それらは完全にダウンロードされているので、3つの画像がすべてダウンロードされて同じ瞬間に表示されるまでには数秒かかるという理由があるはずです。

+0

あなたはIOS tableviews/collectionvieための「遅延ロード・イメージ」になっているはずです私はAlamofireフレームワークを使用することを提案します。ポッドをフレームワークに追加する場合は、「Alamofire」、「AlamofireImage」の両方を追加することを忘れないでください。これを行うと、次のように呼び出すことができます:ootdImage.af_setImage(...) – IxPaka

+0

これを参照してください - https://developer.apple.com/library/content/samplecode/LazyTableImages/Introduction/Intro.html – Santosh

+0

私はこれについてはわかりませんが、firebaseから取得したURLの配列からレイジーローディングイメージをイメージにロードする必要があるため、alamofireImageが本当に役立つとは思いません。 –

答えて

0
func scrollViewDidScroll(scrollView: UIScrollView) 
{ 
    if scrollView.contentSize.height == scrollView.bounds.size.height + scrollView.bounds.origin.y { 
     //call web service here 
    } 

} 

さらに、クラスにUIScrollViewDelegateを追加します。

0

私はこれに対する解決策が見つかりました:すべてのURLとURLの合計数を取得した後、それは(画像はまだこの時にダウンロードされませんすぐに電池をリロードしますalamofireImage、

let databaseRef = FIRDatabase.database().reference() 

      databaseRef.child("palettes").queryOrdered(byChild: "top").queryEqual(toValue: "#000000").observeSingleEvent(of: .value, with: { (snapshot) in 


       if let snapDict = snapshot.value as? [String:AnyObject]{ 

        for each in snapDict as [String:AnyObject]{ 

         let URL = each.value["URL"] as! String 

         self.URLArrayString.append(URL) 
         print(self.URLArrayString.count) 

         self.collectionView?.reloadData() //Reloads data after the number and all the URLs are fetched 
        } 
       } 
      }) 

を使用して

をポイントは、我々は次のコードに私たちをもたらした)alamofireImageは、画像のダウンロードを一つずつ処理してみましょう:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
     let textLabel = cell.viewWithTag(2) 

     let ootdImage = cell.viewWithTag(4) as! UIImageView 
     // find out how to make it such that the data is gathered before being displayed. 

     //ootdImage.image = imageArray[indexPath.row] 

     let url = NSURL(string: URLArrayString[indexPath.row]) 

     let placeholderImage = UIImage(named: "Rectangle")! 

     let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
      size: ootdImage.frame.size, 
      radius: 0 
     ) 

     ootdImage.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2) 
     ) 

     textLabel?.backgroundColor = averageColor 

     return cell 
    } 
関連する問題