Skip to content

Instantly share code, notes, and snippets.

@Psidium
Created April 23, 2015 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Psidium/6d15525d4fba7b191290 to your computer and use it in GitHub Desktop.
Save Psidium/6d15525d4fba7b191290 to your computer and use it in GitHub Desktop.
UIImage from Bundle
//
//
// Created by Psidium on 4/23/15.
// MIT License.
//
// I've created this initializer to load an UIImage from a NSBundle, a XCTest for example
// but I didn't know Apple had implemented this as UIImage(fromBundle: ... )
//
import UIKit
extension UIImage {
public convenience init?(fromBundleWithClassName: AnyClass, imageNamed: String, withType: String) {
let bundle = NSBundle(forClass: fromBundleWithClassName)
let imagePath = bundle.pathForResource(imageNamed, ofType: withType)
if let imgPath = imagePath {
self.init(contentsOfFile: imgPath)
} else {
self.init(named: "")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment