Swift 3 :
func loadImage(named name: String, type:String = "png") throws -> UIImage {
let bundle = Bundle(for:type(of:self))
guard let path = bundle.path(forResource: name, ofType: type) else {
throw NSError(domain: "loadImage", code: 1, userInfo: nil)
}
guard let image = UIImage(contentsOfFile: path) else {
throw NSError(domain: "loadImage", code: 2, userInfo: nil)
}
return image
}
:
let image = try loadImage(named:"test_image", type: "jpg")