[iOS] Lazy loading in iOS

August 5, 2015

Lazy loading in iOS

Hi again, guys.

Lazy loading is a concept that familiar with developers. Basically, lazy loading is that you are about to load image files remotely but don’t affect to the system performance, so that it will enhance the apps’ productivity, comfort and pleasure all the time. As a role of app user, we clearly need the app that is fast, and responsive. Users would not use, and willing to rate poor star for app which is too slow, or worst performance.

So that, we can easily understand the role of Lazy loading is, especially in cases we need to load images remotely using a URL. So, let’s get it done.

HOW IT WORKS

Lazy loading basically is loading images remotely in a asynchronous manner. More effective, we could implement them into each sperated selector in background. And it will not affect to the app’s performance and users will not feel frustration. So the question is: What is background?. Well, background is basically a thead runs asynchronously, it means it will not run on main thread, it will active when main threads have done. So that’s why all the actions on screen as you can see, they seem like smooth.

Well, too much talking, I will show you now. I’m gonna implement Lazy loading in a table view. In cellForRowAtIndexPath, which is delegate of UITableView:

    if(imageData){
        cell.avatar.image = [[UIImage alloc]initWithData:imageData];;
    }else{
        NSIndexPath *indexPath = [is the index you want to pass];
        [self performSelectorInBackground:@selector(downloadImage:) withObject:indexPath];
    }

and in selector named downloadImage:

-(void)downloadImage:(NSIndexPath *)path{
  	NSString *str=[here Your image link for download];
    UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];
    // you set cell.image = image;
     [tableview performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

The idea is simple, right? With Cocoa, we easily call performSelectorInBackground and performSelectorOnMainThread, use them as purpose of their names. There is another way, it is Grand Central Dispatch(GCD), I will talk more about this next post maybe. So instead of calling performSelectorInBackground, we could call:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
        // code here
    });

And instead of calling performSelectorOnMainThread, we could call:

dispatch_async(dispatch_get_main_queue(), ^{ 
            // code here
        });

That’s pretty funny huh? So now you get a bit idea about Thread, that could be a interested aspect too. But let it be next post maybe.

OTHERS LIBRARIES

There are a lots of libs that you can use with many features, we can Lazy loading images remotely, caching images managements, encrypting images, calling images from many platforms. Such as: SDWebImage - which is the most popular one, JMImageCache and many others libs as you can expore them with your purposes.

So, that is the end, happy coding and see you!