SpaceDog SpaceDog - 1 year ago 421
Swift Question

Creating UIImage with renderingMode in Swift

In objectiveC I would do this

UIImage *image = [[UIImage imageNamed:@"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

but in Swift I have tried all alternatives like this, without success

var image : UIImage = UIImage(named:"myImage.png").imageWithRenderingMode(renderingMode: AlwaysOriginal)

It shows an error: use of unresolved identifier 'AlwaysOriginal'

How do I do that?

Answer Source

that would be the proper syntax:

var image : UIImage? = UIImage(named:"myImage.png").imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

but you can use this 'shortcut' as well:

var image : UIImage? = UIImage(named:"myImage.png").imageWithRenderingMode(.AlwaysOriginal)

For Swift 3

var image = UIImage(named:"myImage")?.withRenderingMode(.alwaysOriginal)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download