Ahoura Ghotbi
Ahoura Ghotbi

Reputation: 2896

Loading images from a URL into a UITableViewCell's UIImageView

I have the following code: (Note: newsImageURL is an NSArray)

NSString *imagesURL = @"http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage02.png,http://aud.edu/images/newsimage03.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,";
newsImageURL = [[NSArray alloc] initWithArray:[AllNewsHeadLine componentsSeparatedByString:@","]];

I am trying to load these images into a cell using the code below:

NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]]; 
cell.image = [UIImage imageWithData:imageData];

The image loads fine when I use this line instead:

cell.image = [UIImage imageNamed:@"imagename.png"];

What am I doing wrong?

Upvotes: 4

Views: 13460

Answers (4)

nivritgupta
nivritgupta

Reputation: 1966

You Can Easily load all the images with the Help of Following code ,Details Array is a Main Array

Details Array :-  {
        "item_code" = 709;
        "item_desc" = Qweqweqwe;
        "item_name" = AQA;
        "item_photo" = "http://toshaya.com/webapp/snap&sell/api/img_items/709.png";
        "item_price" = "0.00";
        "item_till" = "20-25";
        "item_type" = Orange;
        latitude = "";
        longitude = "";
    }

With the Help of Following Code Retrieve The Photo-URL into String

 NSString * result = [[DetailArray objectAtIndex:indexPath.row]objectForKey:@"item_photo"]; //componentsJoinedByString:@""];

 NSLog(@"%@",result);

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:result]];
cell.icon.image = [UIImage imageWithData:imageData];

Upvotes: 1

jiachunke
jiachunke

Reputation: 41

Maybe you can have a try https://github.com/SpringOx/ALImageView.git.It is much simpler than SDWebImage.You only need two source files(ALImageView.h/ALImageView.m).You can reuse the image view to reload different urls in a tableview cell.

  1. Support local and memory cache;
  2. Support place holders;
  3. Support tap touch(target-action);
  4. Support corner for the image view;

Upvotes: 2

aporat
aporat

Reputation: 5932

You should use an existing framework which supports caching, default place holders and lazy loading of images.

https://github.com/rs/SDWebImage is a good and simple framework

#import "UIImageView+WebCache.h"

...

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

    // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

Upvotes: 9

reddersky
reddersky

Reputation: 963

You can load data this way:

NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]];

And you can instantiate the array of URLs this way too:

NSArray *newsImageURL = [imagesURL componentsSeparatedByString:@","];

However, if someone scrolls around on the table a great deal, you may end up loading the images many times over as the cells are recycled.

Upvotes: 4

Related Questions