开发者

Fetching images from server while drawing the cell

Why should we avoid loading a image from server while drawing a cell?

In my app I get data from server which contains image URLs as well. Now, I draw my cells and use these image URLs to fetch image from the server and draw it. Now, sometimes image does not get displayed even if image is actually present at that URL as I can see it through browser.

Is there any cell drawing limitation which could cause this issue? Shall I 开发者_如何学Gofetch images when I get data from server.

Does cell rendering happens before image is actually drawn.


    NSString *imgUrl = [ImageURLArray objectAtIndex:indexPath.row];

if(imgUrl != nil)
{
    NSString * ImagePath;

    NetworkManager *manager = [[NetworkManager alloc] init];

    ImagePath = [manager GetFile:imgUrl];

    [manager release];

    manager = nil;

    if(ImagePath != nil)

    {

          UIImage *newImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:ImagePath]];        

        UIGraphicsBeginImageContext(CGSizeMake(50, 50));

        // now redraw our image in a smaller rectangle.
        [newImage drawInRect:CGRectMake(25, 10, 30, 30)];

        newImage  = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        cell.imageView.image  = newImage; 
    }
}
  return cell;

}

hope this will solve your problem


What could be happening is you have a main thread which is the UI display thread. By fetching image from a url from this thread, you are essentially blocking the cell (i.e. scrolling). Also might be the case that the image is in the process of downloading in which case you'll not see it till it downloads.

Generally UITableView does not display the cell till it's ready to be displayed, by doing the image download in the main thread you are harming your performance. What you should do is to launch a background thread which downloads the image from the url & then update the cell of the image contents when the download is ready.

Best way is to have an initial placeholder like a default image or a spinner, which gets replaced when the main image download gets done.

Dont worry you dont need to implement all this. No point in reinventing the wheel. Here's an awesome library which I use for the same - UIImageView + WebCache


fetch images when I you data from server and pass the information to the view. like

ImageView *imageView = [[ImageView alloc] initWithNibName:@"ImageView" bundle:[NSBundle mainBundle]];
imageView.prod=self.prod;
imageView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];
imageView=nil;

hope this will solve your problem


Clearly, the image download is taking more time. You should be using placeholder images until the download completes. You can't stop the user from scrolling.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜