Upload image to table from iphone sdk url

I have a tableView and you need to load the image from the URL. I have an array containing the URLs of the images and when loading the page I need to load all the images into a table. Please note that not one URL has an array with different URLs. How can i implement this? Please, help

Thanks.

+6
source share
6 answers

You can use GCD to load images in the background stream, for example:

//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]; }); }); 

Hey. But you probably need to add dispatch_release (concurrentQueue); so that there is no leakage. - Frank August 25 at 19:43

+34
source

You can use Lazy Loading when you want to download images from the 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]; } 
+7
source

You can use SDWebImage, which makes it very easy and fast to load images into a UITableView.
https://github.com/rs/SDWebImage

+5
source

Try this code, imagearray contains image urls

 -(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; } 
+4
source

You need to create your own custom cell for lazy loading. This will allow you to download images correctly and without freezing. Here is a good example of how to do this: Download Asynch Image

+3
source

With afnetworki, it's too easy.

 //afnetworking #import "UIImageView+AFNetworking.h" [cell.iboImageView setImageWithURL:[NSURL URLWithString:server.imagen] placeholderImage:[UIImage imageNamed:@"qhacer_logo.png"]]; 
-2
source

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


All Articles