I am trying to analyze a photo concurrently using a background thread from GCD. Here is the code I have written:
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) {
for (var i = 0; i < 8; i++)
{
let color = self.photoAnalyzer.analyzeColors(imageStrips[i])
colorList.append(color)
}
}
For clarification on the variable names, here are their descriptions:
photoAnalyzer is an instance of a class I wrote called Analyzer that holds all of the methods to process the image.
analyzeColors is a method inside the Analyzer class that does the majority of the analysis and returns a string with the dominant color of the passed in image
imageStrips is an array of UIImage's that make up the portions of the original image
colorList is an array of strings that stores the return values of the analyzeColor method for each portion of the image.
The above code runs sequentially since the for loop only accesses one image from the imageList at a time. What I am trying to do is analyze each image in imageStrips concurrently, but I had no idea how to go about doing that.
Any suggestions would be greatly appreciated. And if you would like to see all of the code to further help me I can post a GitHub link to it.
EDIT This is my updated code to handle 8 processors concurrently.
dispatch_apply(8, imageQueue) { numStrips -> Void in
let color = self.photoAnalyzer.analyzeColors(imageStrips[numStrips])
colorList.append(color)
}
However, if I try to use more than 8 the code actually runs slower than it does sequentially.
dispatch_applyis what you're going to need to take advantage of concurrent processing.