Thursday, May 22, 2014

Lazy Loading

Lazy Loading

Step 1
First Create  project  like this......

Step 2

And change the Arrays like this.... its in ViewDidload method


----------------------------------------------------------------------------------------------------------


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    profileImageArray =[[NSMutableArray alloc]initWithObjects:
                      @"http://upload.wikimedia.org/wikipedia/commons/e/eb/SLIIT_MLB_1.jpg",
                      @"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGRrSBTxhJEVFgjD-bFIW8Qpqj4XFg4lk7B9J-KiPwdPTJTtHvBnZVKGUECOZc3ojXSj0ksd_jgkL5_fPd0MEYYVlNrQEccBtnvb8yRKQLsiYYXyh_qADCZjO9XJXMxv4_QVPNaMOYnEOp/s220/nwmpic.jpg",
  @"http://www.userlogos.org/files/logos/robinhood/sliit400.png",
     @"https://lh5.googleusercontent.com/-c6f2H9sAeDM/AAAAAAAAAAI/AAAAAAAAATM/9hzjaMbm4dM/photo.jpg",
                      @"https://lh4.googleusercontent.com/-qGRFN6zAkwA/AAAAAAAAAAI/AAAAAAAAAAA/2vec90BT9UQ/photo.jpg",
                      @"http://www.imagesbuddy.com/images/132/2014/01/purple-teddy-bear-happy-teddy-bear-day-2014.jpg",
                      @"https://lh3.googleusercontent.com/-8MeJ9g6xRso/AAAAAAAAAAI/AAAAAAAAAAA/1A6e1NaLimU/photo.jpg",
                      @"https://lh4.googleusercontent.com/-yaYTiufsV6I/AAAAAAAAAAI/AAAAAAAAAAA/TLDDaIWQ-Sk/photo.jpg",
                      @"http://www.interviewmagazine.com/files/2011/06/29/img-simon-van-booy_151144210967.png",
                       @"http://m.c.lnkd.licdn.com/mpr/pub/image-Dnol_IS4AkICXerqwCXULDwKFbuqEAnT7r_bL7l_Ftusk7LmDnobMsk4F33qksdVEyhq/ishara-sunjeewa.jpg",
                       
                    
                      nil];
   
    nameArray =[[NSMutableArray alloc]initWithObjects:
                       @"Dhanushka Adrian",
                       @"Dinithe",
                       @"Nilakshan",
                       @"Niron",
                       @"Jude",
                       @"Malsha",
                       @"Kasun",
                       @"Thisra",
                       @"Suranga",
                       @"Ishara",
                       nil];
   
    companyNameArray =[[NSMutableArray alloc]initWithObjects:
                       @"Vburst Software",
                       @"Vburst Software",
                       @"Vburst Software",
                       @"EZCrM",
                       @"Chello Dairy Products(Pvt)Ltd",
                       @"Vburst Software",
                       @"Vburst Software",
                       @"Vburst Software",
                       @"Vburst Software",
                       @"Vburst Software",
                     
                       nil];

   

}

Step 3

---------------Change the tableView cellForRowAtIndexPath method-----------------------------


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath");
   
    NSString *uniqueIdentifier = @"CustomCell";
   
    CustomCell  *cell = nil;
   
    cell = (CustomCell *) [profileTableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
   
   
   
   
    if(!cell)
    {
       
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
       
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[CustomCell class]])
            {
                cell = (CustomCell *)currentObject;
                break;
            }
        }
    }

    cell.nameLabel.text         = [nameArray objectAtIndex:indexPath.row];

     //image place holder---------------
    [cell.profileImageView setImage:[UIImage imageNamed:@"defaultImgae.jpeg"]];
   
   
    NSString *AllPicUrl=[profileImageArray objectAtIndex:indexPath.row];
   
   
    cell.profileImageView.image = [UIImage imageNamed:[profileImageArray objectAtIndex:indexPath.row]];
   
   
    //Lazy Loading------------------------------------------
   
    dispatch_queue_t imageQueue = dispatch_queue_create("imageDownloder", nil);
    dispatch_async(imageQueue, ^{
        //This is what you will load lazily
       
        NSURL *imageUrl=[NSURL URLWithString:AllPicUrl];
       
        NSData   *imageData = [NSData dataWithContentsOfURL:imageUrl];

       
        dispatch_sync(dispatch_get_main_queue(), ^{
            
            cell.profileImageView.image = [UIImage imageWithData:imageData];;
            [cell setNeedsLayout];
        });
    });
   
    //--------------------------------------------------------
   

    return cell;
   
   
}

Step 4

Run It......!