Skip to content

Instantly share code, notes, and snippets.

@alexj70
Last active April 25, 2017 17:27
Show Gist options
  • Save alexj70/cb47486f5c208680bd8945c133216180 to your computer and use it in GitHub Desktop.
Save alexj70/cb47486f5c208680bd8945c133216180 to your computer and use it in GitHub Desktop.

UIImage for Assets

import UIKit.UIImage
public enum Asset: String, Iteratable {
    case iconBack = "Icon-Back"
    
    var image: UIImage {
        return UIImage(asset: self)
    }
}

extension UIImage {
    convenience public init!(asset: Asset) {
        self.init(named: asset.rawValue)
    }
}

Enum iteration

Using

let image = Asset.iconBack.image
let image = UIImage(asset: .iconBack)    

Unit testing

import XCTest
@testable import LIQR
class ImageAssetTest: XCTestCase {
    func test_ImageAsset() {
        for asset in Asset.hashValues()  {
            XCTAssertNotNil(UIImage(asset: asset), asset.rawValue)
            XCTAssertNotNil(asset.image, asset.rawValue)
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment