4

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?

Thomas Nadin
  • 1,167
  • 10
  • 22
Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65
  • What happens with the code as you have it? Do some of the images load, or do you get a blank space where the image should be every time? – reddersky Dec 05 '11 at 18:43
  • when I try to load it from the URLs the space for image disappears, none of the images are shown and nothing happens basicaly. but when I load it using the `imageNamed` it loads fine – Ahoura Ghotbi Dec 05 '11 at 18:53
  • After your call to load the image data into the NSData object, imageData, have you checked to see if this actually contains any data [imageData length]; – reddersky Dec 05 '11 at 19:02
  • ok well it return NULL, so I am guessing thats where the problem is BUT I cant really figure out why!!! the URLS are there!! – Ahoura Ghotbi Dec 05 '11 at 19:09
  • Ok I replaced [NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row] with a URL instead of reading it from the array and it works fine... so its the array, but can someone tell me why its not working properly? – Ahoura Ghotbi Dec 05 '11 at 19:11
  • 1
    Looking at the line where you instantiate the array you are saying [AllNewsHeadLine componentsSe... whereas, from the example, it should be NSArray *newsImageURL = [imagesURL componentsSeparatedByString:@","] – reddersky Dec 05 '11 at 19:45

4 Answers4

9

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;
}
aporat
  • 5,922
  • 5
  • 32
  • 54
  • 4
    he ask why the code above is not working ...and i want to know it too ... adding a framework is not the correct answer and do not helps – Luca Rocchi Feb 09 '12 at 11:42
4

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.

reddersky
  • 963
  • 1
  • 11
  • 28
2

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;
jiachunke
  • 41
  • 1
1

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];
nivritgupta
  • 1,966
  • 2
  • 20
  • 38