I'm loading images for a user's profile using the SDWebImage library. The weird thing is, when I open a profile with lets say, 5 photos, only the last image is showing in the scroll view (full width profile pictures you can scroll through). However, if I close the view and re-open, all the images are there and look perfect. Any idea as to what's going on? Here's the code:
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)" )
})
}
}
}
You're incrementing p too late, the completion block won't run immediately so your loop is going to have the same value for p every time it runs and your image views will all be stacked on top of each other.
Put the p++
outside of the block.