在objectiveC中我会这样做
UIImage *image = [[UIImage imageNamed:@"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
但是在Swift中我尝试了所有这样的替代方案,但没有成功
var image : UIImage = UIImage(named:"myImage.png").imageWithRenderingMode(renderingMode: AlwaysOriginal)
显示错误:使用未解析的标识符'AlwaysOriginal'
我该怎么做?
答案 0 :(得分:157)
这将是正确的语法:
( Swift 3.x 或 Swift 4 )
var image: UIImage? = UIImage(named:"myImage")?.withRenderingMode(.alwaysOriginal)
(对于 Swift 2.x )
var image: UIImage? = UIImage(named:"myImage.png").imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
但您可以使用此快捷方式'以及:
var image: UIImage? = UIImage(named:"myImage.png").imageWithRenderingMode(.AlwaysOriginal)