Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating UIImage with renderingMode in Swift

Tags:

ios

swift

uiimage

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?

like image 377
Duck Avatar asked Jun 10 '14 15:06

Duck


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is rendering mode in Swift?

The rendering mode controls how UIKit uses color information to display an image. See Providing images for different appearances for creating tintable images with template mode.


1 Answers

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) 
like image 92
holex Avatar answered Oct 13 '22 14:10

holex