我正在使用此块从Parse引入UImage。
for object in objects {
let thumbNail = object["StaffPic"] as PFFile
thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
}
}, progressBlock: {(percentDone: CInt) -> Void in
self.logoImages.append(self.image)
})
}
问题是,它运行ProgressBlock 6次(如果查询中有6个图像)。当它全部完成时,我需要它来运行progressBlock ONCE。
有什么想法吗?
答案 0 :(得分:0)
您的方法适用于异步定期更新,因此您可以更新进度指示器或其他内容。因此,当数据进入时,您会定期获得更新...如果您真的只想知道何时完成,那么我建议您仅在percentDone == 100
...
progressBlock: {(percentDone: CInt) -> Void in
if(percentDone==100)
{
self.logoImages.append(self.image)
}
})
答案 1 :(得分:0)
PFFile
's getDataInBackgroundWithBlock:progressBlock:
的progressBlock
用于获取正在下载的各个文件的进度,而不是用于了解所有下载何时完成的方式。即使仅下载单个文件,它也可轻松称为 more 6次。您绝对不应该将self.image
附加到self.logoImages
,这应该在您从image
创建imageData
后立即在结果块中完成。
for object in objects {
let thumbNail = object["StaffPic"] as PFFile
thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
self.logoImages.append(image)
}
}, progressBlock: {(percentDone: CInt) -> Void in
// Update your UI with download progress here (if needed)
})
}
现在,您似乎想要了解所有这些下载何时完成的方法。我会使用dispatch groups。基本步骤是:
dispatch_group_t
dispatch_group_enter
dispatch_group_leave
dispatch_group_notify
。它看起来像这样:
let downloadGroup = dispatch_group_create()
for object in objects {
let thumbNail = object["StaffPic"] as PFFile
dispatch_group_enter(downloadGroup)
thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
self.logoImages.append(image)
dispatch_group_leave(downloadGroup)
}
}, progressBlock: {(percentDone: CInt) -> Void in
// Update your UI with download progress here (if needed)
})
}
dispatch_group_notify(downloadGroup, dispatch_get_main_queue()) {
// This will be called when all your downloads are complete
}