[英]Issues loading images using SDWebImage
I'm loading images for a user's profile using the SDWebImage library. 我正在使用SDWebImage库加载用户个人资料的图像。 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).
奇怪的是,当我打开一个包含5张照片的配置文件时,滚动视图中仅显示最后一张图像(您可以滚动浏览全宽配置文件图片)。 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. 您增加p的时间太晚了,完成块不会立即运行,因此您的循环每次运行时p的值都将相同,并且图像视图将全部堆叠在一起。
Put the p++
outside of the block. 将
p++
放在块外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.