使用SDWebImage加载图像的问题

使用SDWebImage加载图像的问题

问题描述:

我使用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

你递增p太晚了,所以你的循环会在每次运行时具有相同值的p完成块不会立即运行,你的形象的看法都将是堆叠在彼此的顶部。

p++放在块的外面。

+0

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