2015-10-16 323 views
0

我正試圖將數據訪問到包含某些圖像URL的數組中。無法從數組中獲取數據

每當我嘗試創建我的圖像,我的應用程序永遠不會停止加載,並作出一個循環,順便說一句,我的數組永遠不會停下來填充。

這是我的代碼:

class swipe : UIViewController, KinderDelegate {  

    var urls = [NSURL!]() 
    var pictureApp: String! 

    func fetchdata(int: Int, completion: (()->())?) { 

     for repo in repo { 
        urls.append(NSURL(string: repo.picture_url!)!) 
        dump(repos) 
        i++ 
        NSLog("nombre de line = %i", PicArray.count) 

        pictureApp = repo.picture_url 
     } 

     if let img = imageCache[pictureApp!] { 
        let newCard = Model() 
        newCard.image = img 
        print("image présente") 
     }else { 

      for url in urls{ 
       if #available(iOS 8.0, *) { 
        let newCard = Model() 
        dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) { 
        if let data = NSData(contentsOfURL: url) { 
           let image = UIImage(data: data)! 
           newCard.image = (image: image) 
           dispatch_async(dispatch_get_main_queue()) { 
             newCard.image 
           } 
        } 
        } 
        self.data.append(newCard) 
       }        
      } 
     } 

     if self.data.count == 4 { 
      completion!() 
      return 
     } else { 
      self.fetchData(0, completion: completion) 
     } 
    } 

} 

我如何可以利用它們在我的陣列來創建一些圖像的URL?

+1

不應該是 '回購回購' – Azzaknight

+0

除了Azzaknight,'回購'從哪裏來? – adolfosrs

+0

謝謝大家,實際上我不知道爲什麼我只是沒有使用我的循環「repo for repos」,我嘗試過使用它,並將我的代碼更改爲我的更新代碼,並且所有工作,謝謝:) – f1rstsurf

回答

0

像由Azzaknight的建議,我試圖與循環「回購的回購協議」,並改變了我的代碼是:現在

do { 
    data = try NSData(contentsOfURL: NSURL(string: repo.picture_url!)!, options: NSDataReadingOptions.DataReadingMappedIfSafe) 
    let image = UIImage(data: data!)! 
     newCard.image = (image: image) 
     dispatch_async(dispatch_get_main_queue()) { 
             newCard.image 
            } 
           } 
           catch { 
            print("Handle \(error) here") 
           } 
         } 
       } 
       self.data.append(newCard) 
      }  

一切工作!