使用SDWebImage加载图像时出现问题



我正在使用SDWebImage库加载用户配置文件的图像。奇怪的是,当我打开一个有5张照片的个人资料时,只有最后一张图片显示在滚动视图中(你可以滚动浏览全宽度的个人资料图片)。然而,如果我关闭视图并重新打开,所有的图像都在那里,看起来很完美。知道是怎么回事吗?下面是代码:

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)" )
            })
        }
    }
}

你增加p太晚了,完成块不会立即运行,所以你的循环每次运行都会有相同的p值,你的图像视图都会堆叠在一起。

p++放到块的外面

最新更新