• Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
  • #News
  • #APPS
  • #Apple WWDC
  • #Google I/O
  • #Microsoft Ignite
  • #Let’s Talk
  • #Advertise

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
IOS

Tips for Lazy Loading of Images in IOS

Tips for Lazy Loading of Images in IOS

1. Create a new Xcode Project, Single View Application template and add a tableview.

2. Create a custom table view cell.

3. Download SDWebImage latest framework here. (https://github.com/rs/SDWebImage/wiki/Download-Complied-Framework)

Assuming everything’s done, what’s left for us to do is to include SDWebImage Library and use it in our project.

4. Right-click to your Project Navigator, Add Files to your project.

Add “SDWebImage.framework” from the one you downloaded from Step 3. Check “Copy items into destination group’s folder (if needed)”

5. In our project’s Build Phases, under Link Binary With Libraries, click the ( + ) button.

And add “ImageIO.framework.”

6. In our Project’s Build Settings, search for “Other Linker Flags,” double-click the right area of the “Other Linker Flags,” click ( + ) button and input -ObjC

7. In the source files where you need to use the library,

#import <SDWebImage/UIImageView+WebCache.h>

8. Add an image file that will be used as our “placeholder” image. This image will be shown while the image from the URL has not yet finished loading.

9. Add entries to your array that holds the important information like the URL of the images you want to load.

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

items = [[NSMutableArray alloc] init];

[items addObject:@"http://i0.wp.com/hypebeast.com/image/2013/01/hot-toys-iron-man-3-mark-xlii-collectible-bust_1.jpg?w=1410"];

[items addObject:@"http://t0.gstatic.com/images?q=tbn:ANd9GcT4PZc648WRoXzxEdLQA9zMGqBx93_um_HxvsjgYhoY3AvDtkzI"];

[items addObject:@"http://i0.wp.com/hypebeast.com/image/2013/03/hot-toys-iron-man-3-iron-patriot-collectible-bust-2.jpg?w=930"];

[items addObject:@"http://t3.gstatic.com/images?q=tbn:ANd9GcTf_6e7G9pIiw7ZlRRPfdq63NP-jRA6tmstL1ji-ZFEVnTkDjSp"];

[items addObject:@"http://4.bp.blogspot.com/-KlZshKtr0DM/UWDGlG1YqtI/AAAAAAAAAFc/Qt89IIY3s6s/s1600/iron-man-3.jpg"];

}

10. Edit our tableview’s cellForRowAtIndexPath…

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"MyImageCell";

ImageCell *cell = (ImageCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ImageCell" owner:self options:nil];

for (id currentObject in topLevelObjects) {

if ([currentObject isKindOfClass:[UITableViewCell class]]) {

cell = (ImageCell *)currentObject;

break;

}

}

}

// Here we use the new provided setImageWithURL: method to load the web image

[cell.imageView setImageWithURL:[NSURL URLWithString:[items objectAtIndex:indexPath.row]]  placeholderImage:[UIImage imageNamed:@"yourimagename.jpg"]];

cell.imageSource.text = [items objectAtIndex:indexPath.row];

return cell;

}

11. Hit Run!

  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - February 12, 2014 - 2415 Views
Tags | IOS
AUTHOR
Ranjan

This website is basically about of what we learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures and its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

You Might Also Like

mycodetips Xcode

Tips To Store and Retrieve Your Value From Keychain in IOS

December 16, 2013
iOS Logo

Consuming WCF Web Services in an iOS Application

March 5, 2014
mycodetips-newlogo2

Tips to Loading image from a URL to UIImage object

March 10, 2014
Next Post
Previous Post

Support us

Subscribe for updates

Join 8,278 other subscribers

Latest Posts

  • Exploring Single Point Failure
    Exploring Single Point Failures: Causes and Impacts
  • primitive-datatypes-new
    Exploring the Pros and Cons of Primitive Data Types
  • best practice clean code
    Essential Coding Standards and Best Practices for Clean Code
  • YT-Featured-Templates--lld
    What Business Problems Solves in Low Level Design (LLD)
  • SRP-SingleResopnsibility
    SRP : Single Responsibility Principle in Swift and Objective-C
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

do2day

Do2Day

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

Android Database Interview IOS IOSQuestions Javascript Objective-c Programming Swift Tips&Tricks Web Wordpress

  • Exploring Single Point Failures: Causes and Impacts
  • Exploring the Pros and Cons of Primitive Data Types
  • Essential Coding Standards and Best Practices for Clean Code
MyCodeTips

©mycodetips.com

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.