62

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

178

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)
2
  • 2
    ahhh, fantastic. You pass a kind of object that has a enum value. I thought I just needed to pass the enum value. THANKS!
    – Duck
    Jun 10, 2014 at 15:45
  • 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
    – tomblah
    Aug 11, 2021 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.