MItesh
MItesh

Reputation: 81

Asyncronous image loading in uitableview cell

I am new in iPhone programming. I want to load images in to the UITableView. My images came from the web. I know that I must use ASIHttp Request and then load images to the UITableview. But if I do that then until its load images UITableview hang. If I use asynchronous image loading block then each time when Cell==nil or Reuse the request maid, that I doesn't want to do.

Following line of code I am using in Tableview cellForRowAtIndexpath.

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

        static NSString *CellIdentifier = @"CellIdentifier";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UIImageView *imgViewCellBg;

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.accessoryType=UITableViewCellAccessoryNone;



                   imgViewCellBg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,151)];
                   imgViewCellBg.userInteractionEnabled = NO;
                     imgViewCellBg.backgroundColor = [UIColor whiteColor];
                   imgViewCellBg.tag = 10000;
                   [cell.contentView addSubview:imgViewCellBg];
                   [imgViewCellBg release];

         }
       else{
                   imgViewCellBg = (UIImageView *)[cell.contentView viewWithTag:10000];
            }


     //-------------------------------- For the image Downloding And Displaying ------
            __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:(MY Url)]];
            [request setCompletionBlock:^{
                // NSLog(@"Image downloaded.");
                NSData *data = [request responseData];
                UIImage *imageCountry = [[UIImage alloc] initWithData:data];

                [imgViewCellBg setImage:imageCountry];
                imageCountry = Nil;

            }];
            [request setFailedBlock:^{
                NSError *error = [request error];
                NSLog(@"Error downloading image: %@", error.localizedDescription);
            }];
            [request startAsynchronous];
            //-----------------------------------------------------------------


 return cell;

Upvotes: 2

Views: 1743

Answers (3)

Paras Joshi
Paras Joshi

Reputation: 20551

This is an example with AysncImageView, you can download this class from AynchImageView with Example

You can access this class like bellow code.

- (UITableViewCell *)tableView:(UITableView *)tableView

           cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"ImageCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[[UITableViewCell alloc]
                  initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]
                  autorelease];
        } else {
        AsyncImageView* oldImage = (AsyncImageView*)
                 [cell.contentView viewWithTag:999];
        [oldImage removeFromSuperview];
        }

        CGRect frame;
        frame.size.width=75; frame.size.height=75;
        frame.origin.x=0; frame.origin.y=0;
        AsyncImageView* asyncImage = [[[AsyncImageView alloc]
                   initWithFrame:frame] autorelease];
        asyncImage.tag = 999;
        NSURL* url = [imageDownload
                   thumbnailURLAtIndex:indexPath.row];
        [asyncImage loadImageFromURL:url];

        [cell.contentView addSubview:asyncImage];

        return cell;
    }

I hope this help you.

Upvotes: 1

Nitin Gohel
Nitin Gohel

Reputation: 49730

As per my suggestion I am doing asynchronous using SDWebImages A Link of GitHubproject

And I configure this like below steps:-

  • you need to first right click on your project name:->add files to yourProject-> selected SDWebImageproject and add them

NOTE:- please do not check copy option

  • now click on project name in Your xcode going to build phases:->target dependencies:-> click on + button and add SDWebimage ARC

  • now select link binary with library click + button add libSDWebimageARC.a and again click + and add imageIO.framework and also add libxml2.dylib thats it

    going to Build setting:->other link flag:-> add -ObjC

    and header search path add this three item

    1 /usr/include/libxml2

    2 "$(OBJROOT)/UninstalledProducts/include"

    3 "$(TARGET_BUILD_DIR)/usr/local/lib/include"

now build and run its working like smoothly cheers.... :)

It's more easy and very useful task for loading images and its store catch also so you got more speedy work.

And you can asynchronous images with just two line of code like

In .m :-

#import <SDWebImage/UIImageView+WebCache.h>

 NSString *strUrlSting =[d valueForKey:@"Current_RequestUser_Image"];
        strUrlSting =[strUrlSting stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
        NSURL* url = [NSURL URLWithString:strUrlSting];

        [imgView_user1 setImageWithURL:url
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

Upvotes: 1

Loquatious
Loquatious

Reputation: 1775

Just add the AsyncImageView files from https://github.com/SIMHAM/AsyncImageView-1

and then you can add imageView in your cell like below:

AsyncImageView *asyncImageView=[[AsyncImageView alloc] initWithFrame:CGRectMake(15,20,80.0f,110.0f)];
[asyncImageView loadImageFromURL:[NSURL URLWithString:yourImageUrl]];
[cell.contentView addSubview:asyncImageView];

Upvotes: 1

Related Questions