vokal / Cat2Cat

Creates a category on UIImage from Asset Catalogs in order to reduce typos and enable auto-complete with image names.
MIT License
46 stars 11 forks source link

Cat2Cat (Catalog to Category)


Cat2Cat exists to help solve the typo problem with UIImage's imageNamed: method.

Without Cat2Cat, you can be left wondering where your background image named backgroundImage is, only to discover that you've set up a [UIImage imageNamed:@"backrgoundImage"]; call by accident.

With the advent of the Asset Catalog in Xcode 5, there was a huge step in the right direction - images ceased to be tied to their filenames, and it became straightforward to centralize your image assets.

That nasty typo problem, however, still persisted. Until now.

Cat2Cat goes through provided Asset Catalog files and writes out their contents to a UIImage+AssetCatalog or NSImage+AssetCatalog category - each .imageset within an asset catalog will get its own method to call it, prefixed by ac_ to indicate the method is from the asset catalog and to help prevent any namespace collisions.

After running Cat2Cat and adding the category or categories it produces, instead of calling [UIImage imageNamed:@"backgroundImage"], you can now call [UIImage ac_backgroundImage] ensuring that you're always going to get the image you think you're getting, and giving you the benefit of autocomplete when you're trying to remember what in the hell you named that icon.

Cat2Cat is compatible with Xcode 5 projects which can leverage Asset Catalogs (i.e., iOS 6 and above).

Usage


Note: You should not use Cat2Cat as a Git Submodule due to issues with how submodules handle the test filenames which include accents. If you wish to keep an eye on versions through dependency management instead of manually, please use our CocoaPod, which does not contain any of the test projects.

Using a Pre-Compiled Binary And A Build Script

The current compiled binary can be downloaded from the releases page.

usage: Cat2Cat [options]
    -p, --base-path        Base path used for interpreting the asset catalogs and output directory
    -a, --asset-catalog    Asset catalog(s)
    -o, --output-dir       Output directory

        --objc             Output Objective-C category or categories
        --swift            Output Swift class extension(s)

        --ios              Output for iOS (UIImage)
        --osx              Output for OS X (NSImage)

    -h, --help             Show this message

Examples:

Cat2Cat --base-path="/Users/YourName/Desktop/YourProjectFolder" --asset-catalog="Resources/Images.xcassets" --asset-catalog="Resources/Media.xcassets" --output-dir="Categories" --objc --ios --osx
Cat2Cat --swift --ios \
    --base-path="/Users/YourName/Desktop/YourProjectFolder" \
    --asset-catalog="Resources/*.xcassets" \
    --output-dir="Categories"

Please see the iOS Example App's Cat2Cat aggregate build target for the appropriate run script for iOS only, and the Mac Example App's Cat2Cat aggregate build target for the appropriate run script for Mac only.

Directly from Xcode

Download the zip of this repo using the handy link at the right. Open the .xcodeproj, select the default build scheme, then select Edit Scheme...

In the Arguments section, add five (or more) Arguments Passed On Launch to the build scheme:

After you've added your launch arguments, build and run the application. Your new category or categories should be output to the file path you've provided.

Notes


Limitations


Contributors