When we first got started with making the Mac Editor App, we decided we were going to reuse some code from the current alpha of the iOS game that this editor is going to be a utility to, since both the macOS and iOS versions would share some images we generate dynamically.
First issue – NS/UIImage Differences Top
The first thing we had to overcome was the differences in the APIs between macOS and iOS. On iOS it deals with UIImage and has some technical differences with how it stores and loads information (specifically Color Profiles). On macOS (OS X) the image API deals with and wants NSImages which has some additional requirement of having to create Color Profiles or at least give hints as to which one you want for your image).
Second Issue – NS/UIColor Differences Top
Similar to images, the API expects NSColor or UIColor for macOS and iOS, respectively and also contains the issues with respect to Color Profiles. One of the big issues with NS/UIColor is that they are not compatible due to the color profile issues. On iOS there are very few color profiles (if not only 1) and on macOS there can be many. This posed a big problem with cross platform code. Solution: A custom struct and CGColor to the rescue!
That got us all of the basic colors we needed and made it cross-platform enough that the code would compile on both iOS and macOS. CGColor allowed us to use a Color Space profile that is available on both platforms and then use said colors to draw to the dynamically created images.
Tying it all together Top
Finally we had to find a way to tie all of our code together in a cross platform way, enter CGImage which we found works on both macOS and iOS (an NS/UIImage can be created with CGImages) which worked very well for our idea of having some cross-platform image creation (read that as Apple OSes such as macOS, iOS, tvOS and watchOS)
The main goal of the Image manager we created was to allow for the images to be created on the fly in a self contained class which would allow for code to be reused. It took a bit of work but we got it working, and for each OS to get the image type we wanted, we used the awesome Swift language feature called extensions which allowed us to have the Image manager only deal with CGImage and then extend for each OS to return NSImage for macOS and UIImage for iOS.
That’s all for now!