I\'m using the PinRemoteImage library to download images that populate a collectionView
. I want to update the cell height dynamically based on the image height,
I have never used the PinRemoteImage image library, but replying on completion blocks when your downloading from iCloud doesn't work, cause the daft process backgrounds and then fires your completion clause BEFORE it has downloaded all the data. This is an extract of my CloudKit code I needed to implement to get around that.
It is using an NSOperationalQueue, code here. Warning this code will enter an endless loop if any of your image downloads fails, creating zombie at best, stopping your app at worst.
Note self.filesQ.returnQThumbs is a subroutine that checks the size of the images downloaded, if they are greater than zero it has one, if they are zero/nil its still downloading...it returns the number it found in the array. self.filesQ.qcount() returns the number of images it is looking to download.
func preThumb() {
newQueue = NSOperationQueue()
let operation2 = NSBlockOperation(block: {
print("Downloaded, you can now use")
})
operation2.completionBlock = {
print("Operation 2 completed")
}
let operation1 = NSBlockOperation(block: {
var allclear = 0
while (allclear != self.filesQ.qcount()) {
allclear = self.filesQ.returnQThumbs().count
}
})
operation1.completionBlock = {
print("Operation 1 completed")
// your code to display images could go here!
self.newQueue.addOperation(operation2)
}
operation1.qualityOfService = .Background
newQueue.addOperation(operation1)
}
Obviously, perhaps not you need to run this method BEFORE you start downloading ideally.