0

I am displaying images in UITableview using json data and get images. But the data is displayed in UITablevewcells. But images are not displyed. It will take time and after images are displayed. so I want to displayed the images for my uitableview asynchronously using GCD.But I have no idea of asynchronous GCD Concept.Please give me any idea how to applicable asynchronous GCD concept in my coding. please give me guidance any body. This is my code.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{    
   [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
   NSError *err;
   NSString *strResponse=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
   NSLog(@"response is %@",strResponse);
   dit=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&err];
   NSArray *arrResults = [dit valueForKey:@"classifieds_mst"];
   listOfObjects = [NSMutableArray array];

for(dictRes in arrResults) {
 Attributes *at = [[Attributes alloc]init];      
at.classimage=[dictRes valueForKey:@"image_name"];
[listOfObjects addObject:at];
 }

[tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *identifier=@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
     }
    classifiedimage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 80)];
    [cell.contentView addSubview:classifiedimage];
     NSString *str;
    str=att.classimage;
    if(str==nil) {
        classifiedimage.image=[UIImage imageNamed:@"image.png"];
    } else{
    url=@"My url";
     NSString *addurl=[url stringByAppendingString:str];
    classifiedimage.image = [UIImage imageNamed:addurl];
     }
return cell;
}
user987339
  • 10,519
  • 8
  • 40
  • 45
  • Any body give me guidance please –  Jan 16 '14 at 06:14
  • please refer this [link][1] [1]: http://stackoverflow.com/questions/15668160/asynchronous-downloading-of-images-for-uitableview-with-gcd?rq=1 hope this will help you. – hpp Jan 16 '14 at 06:41

3 Answers3

1

You can try this one, for asynchronously loading images in cellForRowAtIndexPath method, Here I am getting the url of images and use the lazy loading.

dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(concurrentQueue, ^{

    dispatch_async(dispatch_get_main_queue(), ^{

        NSString *imageStr=[@"urlLink" stringByAppendingString:[populerPostImageArray objectAtIndex:indexPath.row]];

        [cell.myImgView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"nopic.png"]];
    });
});
Harunmughal
  • 367
  • 1
  • 4
  • thanks for your response.But i got error this line of code [cell.myImgView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"nopic.png"]]; The error message will display like this no visible interface for UIImageview declares the selector setImageWithUrl:placeholiderImage." –  Jan 16 '14 at 06:51
  • Actually I have created a custom cell class and myImgView declared in that class.You can use cell.imageView and also use your own image as the placeholderImage not nopic.png.Thanks – Harunmughal Jan 16 '14 at 08:53
0

you can use Lazy loading concept for loading images. for this refer below link: https://github.com/rs/SDWebImage

Shubham
  • 570
  • 3
  • 12
  • thanks for your response.But please give me any solutions. you send the github code is very difficult to understand.Please give me solution based on my code. –  Jan 16 '14 at 06:16
  • 1
    you can just add SDWebImage folder in your application and import #import "UIImageView+WebCache.h" this and put below line of code inside your cellForRowAtIndexPath method: [cell.imageView setImageWithURL:[NSURL URLWithString:@"your image path"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; – Shubham Jan 16 '14 at 06:27
  • I think you should have reputation to me because I have suggested to you for the Lazy Loading concept and also provided you the code. – Shubham Jan 16 '14 at 08:25
0

If you need to load and cache your images, you can use APSmartStorage. It supports downloading, memory and disk cache with automatically purge on memory warning. Works like a charm:)

Krivoblotsky
  • 1,492
  • 11
  • 17