2015-07-21 61 views
0

我使用SDWebImage庫加載用戶簡檔的圖像。奇怪的是,當我打開配置文件並顯示5張照片時,只有最後一張圖像顯示在滾動視圖中(您可以滾動瀏覽全寬配置文件圖片)。但是,如果我關閉視圖並重新打開,所有圖像都在那裏,看起來很完美。關於發生什麼事的任何想法?下面的代碼:使用SDWebImage加載圖像的問題

func getPhotos(user:PFUser, forKey:NSMutableArray) { 

    var p = 0 

    for f in forKey { 

     if let pic = user.objectForKey(f as! String) as? PFFile { 

      var width = Int(phonewidth) 
      photoscroll.contentSize = CGSize(width: CGFloat((p + 1) * width), height: phonewidth) 
      pageControl.numberOfPages = p + 1 

      var position = CGFloat(p*width) 

      var imgview = UIImageView(frame: CGRectMake(position, 0, phonewidth, phonewidth)) 



      imgview.sd_setImageWithURL(NSURL(string: pic.url), completed: {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: NSURL!) in 
       imgview.image = image 
       self.photoscroll.addSubview(imgview) 
       self.userpics.append(image) 
       p++ 
       println("Added: \(f), URL: \(pic.url)") 
      }) 
     } 
    } 
} 
+0

在圖像下載完成塊中放置一個斷點。破發點是否受到打擊? – Antonio

+0

你的意思是增加「p」變量後? – cb428

回答

0

你遞增p太晚了,所以你的循環會在每次運行時具有相同值的p完成塊不會立即運行,你的形象的看法都將是堆疊在彼此的頂部。

p++放在塊的外面。

+0

啊,非常感謝!工作。 – cb428