2

Here is my code displaying images from server. After the image is loaded, when you scroll down and then back, the image disappears and there's another delay before it's loaded again.

  dispatch_async(kBgQueue, ^{

        NSString *profileImageStr = [[displayItems objectAtIndex:indexPath.row] objectForKey:@"image_url"];
        NSURL* profileImgURL = [NSURL URLWithString:profileImageStr];
        NSData *profileImgData = [NSData dataWithContentsOfURL:profileImgURL];


        NSString *postPhotoStr = [[displayItems objectAtIndex:indexPath.row] objectForKey:@"image_url"];
        NSURL *postImgURL = [NSURL URLWithString:postPhotoStr];
        NSData *postImageData = [NSData dataWithContentsOfURL:postImgURL];


        dispatch_async(dispatch_get_main_queue(), ^{


            if (postImageData && profileImgData != nil) {


                cell.thumbnailImg.image = [UIImage imageWithData:profileImgData];




                cell.bgImgView.image = [UIImage imageWithData:postImageData];
            }else
            {


                cell.thumbnailImg.image = [UIImage imageNamed:@"noimage.jpg"];




                cell.bgImgView.image = [UIImage imageNamed:@"noimage.jpg"];
            }

        });

    }
                   );
    return cell;
danh
  • 62,181
  • 10
  • 95
  • 136
user2838188
  • 165
  • 1
  • 12
  • 2
    Sentences all in capitals are very difficult to read – Phil Wilson Oct 07 '13 at 03:42
  • You need to store images in your local path, while download finished. here each time images are downloading. so it will disappear. – karthika Oct 07 '13 at 04:47
  • how will I do that please? – user2838188 Oct 07 '13 at 05:05
  • @user2838188 please read this to learn how to store files: http://stackoverflow.com/questions/5323427/how-do-i-download-and-save-a-file-locally-on-ios-using-objective-c . You have to save file after it's downloaded and check if some image is cached in device. If yes, load from device, if not - from internet. – Tomasz Szulc Oct 07 '13 at 05:53

1 Answers1

1

I think you need to implement cache for image. Store image in NSCache.

//instance 
NSCache *imageCache;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *simpleTableIdentifier=nil;
    if([objRepresentative.team count]>0)
    {
        simpleTableIdentifier=[NSString stringWithFormat:@"%@",[objRepresentative.employeeID objectAtIndex:indexPath.row]];
    }
    else
        simpleTableIdentifier = @"ContactTableCell";


     ContactTableCell *cell = (ContactTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ContactTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        NSString *photoString=[NSString stringWithFormat:@"http://www.foo.com/%@",[searchPhoto objectAtIndex:indexPath.row]];

            UIImage *image = [imageCache objectForKey:photoString];
            if (image)
            {
                cell.ProfilePicture.image=image;
            }
            else
            {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


                    NSURL *url = [NSURL URLWithString:[photoString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];

                    if(image)
                    {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            UIImage *small = [self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];
                            cell.ProfilePicture.image=small;
                        });

                        [imageCache setObject:image forKey:photoString];
                    }
                    else
                        cell.ProfilePicture.image=[UIImage imageNamed:@"noPicture.png"];

                });
            }
    }
return cell;
}
MD.
  • 1,157
  • 11
  • 18