64

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?

1 Answer 1

181

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)
Sign up to request clarification or add additional context in comments.

2 Comments

ahhh, fantastic. You pass a kind of object that has a enum value. I thought I just needed to pass the enum value. THANKS!
uggh, I wish Xcode's code complete would be just a bit smarter, e.g. if you type ".imageWithRenderingMode" it really should predict ".withRenderingMode"...would save countless google searches

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.