iOS programming
fetching the image and implementing the scrollView
This one yet another article about the things I’ve learned in iOS. I’m still in the middle of doing the Stanford iOS course which is by the way one of the best I’ve encountered so far.
In this article I will revisit all the steps necessary for implementing the image in one of the viewControllers as well as what’s needed for the scrollView to work. In the same time I must again confess that the whole article is primarily for educational purposes and I use it to enforce what I’ve learned.
Lets start!
First we need to open a new project in Xcode — a single view application. As next we add another class that is a subclass of the UIViewController
Then we need to set the properties:
We need one for UIImageView and UIImage. Our next task will focus on implementing the setters and getters of these objects. We start with UIImage.
What’s very interesting in the UIImage getter is that it doesn’t return the actual image of the UIImage. Instead we use the image of the UIViewImage object. Not using the instance variable has got an interesting advantage that I will reveal shortly.The setter besides assigning again the image from the UIImageView we invoke a method ‘sizeToFit’ so then whenever our image is set it will adjust it size accordingly.
Whenever we implement setters and getters manually there is a hidden agenda to it. In most cases you add some additional setup to it so that when the object is created it already has some additional features. In this case its the s’izeToFit’ method as well as the connection with the other objects image (UIImageView).
Because of the fact that we haven’t really used the ivars in here we don’t have to synthesize the setters and the getter. The rule then is: if the setter and getter is implemented manually (due to some additional features) you would have to add a synthesize line for ivars. In this case however we don’t.
As next we lazy instantiate the UIImageView:
Then in the public API we add another property creating an NSURL object:
In the imageURL setter we will assign the _imageUrl ivar but most of all we will get the image from the web:
It’s interesting that we are assigning the imageUrl to the image. We invoke a class method ‘imageWithData’ and then pass NSData object which is create from the contents of URL, which is actually our public object — imageUrl.
Now that we implemented the setters and getters we have to add our ‘imageView’ onto the scene. To do that we invoke the ViewDidLoad method in the imageUIViewController.m file:
Now the easy bit — storyBoard. We need to add a navigation Controller into the StoryBoard. There are actually two ways to do that.
First: we can click on the current viewController and from the top menu select: Editor -> Embed In -> Navigation Controller.
This will create the navigation controller with the segue set onto our View Controller.
Get Marek Tomaszewski’s stories in your inbox
Join Medium for free to get updates from this writer.
Second: we can just drag the navigation controller from the object menu on the right onto the canvas. With this method however we have to delete the the rootViewController which is by default assigned to the navigation controller:
and Ctrl+drag from the navigation controller to our desired viewController, which will create the segue.
As next we add 3 buttons onto our primary view Controller and add another generic viewController next to it. Then in the customClass menu we assign the ImagineViewController class to it.
This will create the connection between the viewController in the storyboard and the model itself.
As next we Ctrl+drag from the buttons to the ImageViewController. This will create 3 push segues. Once we click one of them we can set the identifier to each one of them.
Now we need to implement the ‘prepareForSegue’ method so that when the button is clicked we will segue onto the next scene.
The best place to implement the method would be the main viewController.m file:
The code for the prepareForSegue is pretty straight forward. First we check if the destination viewController is from the class specified by us — in this case its ImageViewController. Then we create an object which will grant us access to the imageUrl instance variable. Through ‘URLWithString’ class method we pass the url from which we fetch the image. If the photos we’ll use have got a the same url then there is a great chance to scalable our model. By putting the token instead of the actual image name we can just adjust each segue.identifier (assigned earlier in the storyBoard) so that one line of code will work across 3 different image. The only required thing in this concept is the same address for all of the images. Finally we access the ‘title’ of the Segue and assign it to the identifier as well.
All the segues are now prepared to act. Whenever we click one of the buttons it will transfer as to the next scene in which the image will be downloaded from the web. It’s worth mentioning that the images are stored only for the time of the session in a temporary file.
Now’s the time to implement the scrollView. There are only two things to remember when we want to add a scrollView to our application:
- We need to add the imageView onto the scrollView property.
- We need to ensure that the contentSize of the scrollView is equal to the size of the image we display.
Let’s drag the UIScrollView object onto the scene. Then lets create a property by Ctrl+dragging from the scrollView into our code.
Then we change the line in viewDidLoad to:
This will ensure that the image is add onto the scrollView. Secondly in the image:
and scrollView setter we implement the same lines:
The contentSize of the scrollView is set to the image size. On the right side of the equal sign we apply the conditional statement in which: if the self.image is not nil then it will return us the size of the image. Otherwise it will return the ZERO size.
The reason why we put the line in setter of the scrollView is that this particular setter is set before the outlets are set. When the method prepare for segue is invoked it happens before the outlets as well so the image size is nil just then.
When we run the simulator and click one of the buttons the next scene will present us with an image put in the scrollView.







