Creating UIImage with renderingMode in Swift

IosUiimageSwift

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

that would be the proper syntax:


(for Swift 3.x or Swift 4)

var image: UIImage? = UIImage(named:"myImage")?.withRenderingMode(.alwaysOriginal)

(for Swift 2.x)

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDuckView Question on Stackoverflow
Solution 1 - IosholexView Answer on Stackoverflow