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?

Duck
  • 34,902
  • 47
  • 248
  • 470

1 Answers1

179

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)
holex
  • 23,961
  • 7
  • 62
  • 76
  • 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 '14 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 '21 at 05:23