Loading images from a URL in a UITableViewCell 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 following code:

 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?

+4
source share
4 answers

You should use existing infrastructure that supports caching, default location owners and lazy loading of images.

https://github.com/rs/SDWebImage is a nice and simple structure

 #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; } 
+9
source

You can load data this way:

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

And you can also create an array of URLs:

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

However, if someone scrolls around the table a lot, you can end up loading images many times when the cells are recycled.

+4
source

Perhaps you can try https://github.com/SpringOx/ALImageView.git . This 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.

  • Support for local and cache memory;
  • Support place holders;
  • Support touch touch (target action);
  • Support angle for image presentation;
+2
source

You can easily load all images using the help of the following code, Details Array is the 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 = ""; } 

Using the following code Extract the url of the photo into a 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]; 
+2
source

Source: https://habr.com/ru/post/1384753/


All Articles