Knobility Source
< Back
Knobility will be fully "free software" and "open source" at some point -- I want this
to become "bigger than just me," with "mods" and who knows what else -- but for now
I'm only ready to share parts that describe the file format, for people who want
to be able to extract things from the files and/or create them.
The .knb file is literally just a hierarchy of UIView-subclassed objects -- hierarchy
as in subviews of the main "desktopImageView = DesktopObject()" --
stored via NSKeyedArchiver. The following Swift 2 class files describe these objects and their NSCoding
properties:
The saving is just...
NSKeyedArchiver.archiveRootObject(self.desktopImageView, toFile: path)
and then the loading is likewise...
view = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as! UIView!
...Athough, little tip here: NSKeyedUnarchiver can seem notoriously slow (like 5 to 20 seconds!) if it's used in its
(default) (semi-)asynchronous fashion. However, if you prefer not to wait around forever to
actually see what you loaded, then wrap
the loader and any subsequent 'immediate' usage/displaying stuff inside a
dispatch_async block where you grab the main thread. Something like...
dispatch_async(dispatch_get_main_queue(), { // this is the magic that makes it 'fast'
view = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as! UIView!
self.addMultipleObjectsToGear(view) // or whatever
view.setNeedsDisplay() //etc
})
Ok, have fun! Let me know what you come up with!
-Scott Hawley, 9/13/2015
P.S.- One last thing: Seems that NSCoding can be a little stingy about encoding images stored
"Assets" subdirectories of Xcode projects. So, the .knb file generally may NOT contain
the image of the knob, that's why KnobObject.swift is always creating a knob from scratch. Here's the image in question: link to image of default knob.