The following task is given:
- Scan a folder, which contains subfolders with images inside and ensure that none of the images is corrupted
- Use MacOS and Swift
- Open every image and check it against corruption
I wrote this tiny command line program:
import ArgumentParser
import AppKit
import Foundation
struct CheckImages: ParsableCommand {
@Option(help: "The images root directory")
var path: String
func run() throws {
let directories = try FileManager.default.contentsOfDirectory(atPath: path)
for directory in directories {
if directory == ".DS_Store" {
continue
}
let prefix = self.path + "\(directory)/PREFIX_\(directory)"
let imageName = prefix + ".jpg"
let image = NSImage(contentsOfFile: imageName)
if image == nil {
print("PROBLEM \(imageName)")
}
}
}
}
CheckImages.main()
Each image is around 20MB in size. Altogether I have ~150.000 images to check.
Unfortunately XCode terminates the program with Program ended with exit code: 9
. Digging deeper (with Instruments) it turns out that this little help application consumes all my memory in NSImage.init()
. As NSImage
is a mature object, I doubt that there is any problem with it. Thus, my question is, can anybody explain this behaviour to me?
My environment:
- XCode Version 11.4.1 (11E503a)
- Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)