Load image to a tableView from URL iphone sdk
Asked Answered
D

6

6

I have tableView and need to load image from URL. I have an array that contains the URLs of images and when the page loads I need to load all the images into the tableview. Note that, not from a single URL, have an array with different URLs. How can I implement that? Please help

Thanks.

Decision answered 27/9, 2011 at 6:36 Comment(1)
The best solution I found: https://mcmap.net/q/151665/-asynchronous-downloading-of-images-for-uitableview-with-gcd!Regality
W
34

You can use GCD to load images in background thread, like this:

//get a dispatch queue
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //this will start the image loading in bg
    dispatch_async(concurrentQueue, ^{        
        NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL];

        //this will set the image when loading is finished
        dispatch_async(dispatch_get_main_queue(), ^{
            imageView.image = [UIImage imageWithData:image];
        });
    });

Hi. But you probably need to add a dispatch_release(concurrentQueue); to be sure no leak. – Franck Aug 25 at 19:43

Wivern answered 27/9, 2011 at 6:43 Comment(2)
Hi. But you probably need to add a dispatch_release(concurrentQueue); to be sure no leak.Putput
Side question: do you need @autorelease pool when using dispatch_async?Neau
D
7

You can use Lazy Loading when you want to download Images from internet

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //All you reusable cell implementation here.
   //Since your Images sizes differ. Keep a custom Imageview

    if(![imagesForCategories containsObject:indexPath])
    {    
        customImageView.image = [UIImage imageNamed:@"default-image.png"];
        NSMutableArray *arr = [[NSArray alloc] initWithObjects:[imageUrlArray objectAtIndex:indexPath.row],indexPath, nil];
        [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];
        [arr release];
    }
    return cell;
}

- (void) loadImageInBackground:(NSArray *)urlAndTagReference 
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSURL *imgUrl=[[NSURL alloc] initWithString:[urlAndTagReference objectAtIndex:0]];
    NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
    UIImage *img = [UIImage imageWithData:imgData];
    [imgUrl release]; 
    NSMutableArray *arr = [[NSMutableArray alloc ] initWithObjects:img,[urlAndTagReference objectAtIndex:1], nil  ];
    [self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
    [arr release];
    [pool release];
}

- (void) assignImageToImageView:(NSMutableArray *)imgAndTagReference
{
    [imagesForCategories addObject:[imgAndTagReference objectAtIndex:1]];
    UITableViewCell *cell = [celebCategoryTableView cellForRowAtIndexPath:[imgAndTagReference objectAtIndex:1]];
    UIImageView *profilePic = (UIImageView *)[cell.contentView viewWithTag:20];
    profilePic.image = [imgAndTagReference objectAtIndex:0];

}
Dinky answered 27/9, 2011 at 7:10 Comment(1)
You can also see this apple sample code for lazyloading developer.apple.com/library/ios/#samplecode/LazyTableImages/…Dinky
T
5

You can use SDWebImage which permits very easy and speed loading of images in UITableView.
https://github.com/rs/SDWebImage

Tingly answered 27/9, 2011 at 7:49 Comment(0)
D
4

Try this code,imagearray contains urls of image

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [imagearray objectAtIndex:row]]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];


    cell.imageView.image =image;
    return cell;
}
Dg answered 27/9, 2011 at 6:41 Comment(4)
This will freeze Interface while image downloadingHousemother
thanks, and its working fine. But the image sizes are different.Decision
and also, running slowly, is there any idea to make it run fast, sometimes its freezing.Decision
If there is large amount of data(images),you need to download it asynchronously...Chk this link markj.net/iphone-asynchronous-table-imageDg
H
3

You need to create your custom cell for lazy loading. This will allow you to download images correctly and without freezing. Here is nice example how to do this: Asynch image loading

Housemother answered 27/9, 2011 at 7:0 Comment(0)
D
-2

With afnetworki, it is too easy.

//afnetworking

#import "UIImageView+AFNetworking.h"

 [cell.iboImageView setImageWithURL:[NSURL URLWithString:server.imagen] placeholderImage:[UIImage imageNamed:@"qhacer_logo.png"]];
Dogwatch answered 8/1, 2015 at 0:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.