How would I measure the progress of a forEach
loop and display it in a progress bar?
I will be rotating vertical images to make them horizontal. The number of images to be converted will be different every time.
Here is what I am doing to convert the images which works fine, I just need a way to display progress to the user:
func rotateandSave(files: [URL]) {
files.forEach { file in
let path = file
let image = NSImage(contentsOf: path)
let rotatedImage = image?.rotated(by: rotateAngle)
saveImage(image: rotatedImage!, destination: path)
}
}
Do this:
func rotateandSave(files: [URL]) {
DispatchQueue.global(qos: .utility).async {
files.forEach { file in
let path = file
let image = NSImage(contentsOf: path)
let rotatedImage = image?.imageRotated(by: 90)
saveImage(image: rotatedImage!, destination: path)
DispatchQueue.main.async {
self.progressBar.increment(by: 1)
}
}
}