Every mobile application have at least one image which is displayed, whether is profile photo, photo album or product photo. This component is called UIImage. UIImage is component which is contained in every view component from UIKit. If we see in Attributes Inspector, every component have property background image or in case of buttons is also image for state, so this component is responsible for keeping image data and displaying it in component where is implemented.
Most of the time in the applications we will use UIImageView view component dedicated for displaying images. In this tutorial we will develop one example with UIImageView and UISwitch component which we saw how is working in the first tutorial, Using UISwitch component into your iOS App.
We are starting with creating the Single View Application in our Xcode IDE.
Next we are setting Product Name, Organisation Name, Identifier, Language ( Objective-C or Swift ) and Device.
Your project should look like this after finishing previous step:
Next we are setting the size of the view and we are choosing compact, portrait for all iPhones.
We are dropping from the Object Library one UIImageView and one UISwitch components.
If our image have transparent background we can set some background color or to remove default white background color we can choose Clear Color option.We can also set manually alpha channel of the component from 1.0 to 0.0.
Other important property is how we want this image to be displayed, are we want our image view to be automatically scaled to display the image in full resolution or we want the image to occupy the whole image view or maybe display centered, there are plenty options for how we want image to be displayed in out component UIImageView.
There is also property which allow us to have direct interaction with the UIImageView component where UIImage is displayed and is called User Interaction Enabled which should be checked if we want to make interesting custom components based on UIImageView, like check boxes for example but for customizing components and developing custom view components we will see in some of the next tutorials.
Next from the InterfaceBuilder in Assistant Editor we are creating IBOutlet for label which will be used for displaying selected value in our example.
With dragging from the InterfaceBuilder to Assistant Editor we are making one action method which will be executed by changing the state On / Off on our UISwitch component we will display different image in our UIImageView component.
Now we are clicking on Assets group.
And we are dropping two or more images.
Next we are going back in our ViewController and we are initializing UIImageView with default image which will be equal to UISwitch component On state.
In the method which will be executed on UISwitch component state change we are writing simple logic for changing the image depending on the UISwitch state.
When we compile and run the application in the simulator we can try changing UISwitch component state so we can see if everything we have implemented correctly how its working.
Complete source code can be found on my GitHub ready for testing.
(note: emoji png icons are downloaded from Google Images and are free for download)