2

I have am downloading multiple images from a web server at the same time, and I the problem is they are getting mixed up with each other.

dispatch_async(dispatch_get_global_queue(0,0), ^{
    NSData * data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"website.com"]];
    if (data == nil)
        return;
    dispatch_async(dispatch_get_main_queue(), ^{
        cell.imageView.image = [UIImage imageWithData:data];
    });
});

I got this code from: Getting Image from URL Objective C.

How do i fix this?? also is there any way to speed up download speeds?

Community
  • 1
  • 1
Jason Silberman
  • 2,471
  • 6
  • 29
  • 47
  • Nobody will be able to help you unless you post (the rest of) your code. How is this code called? What is `cell`? Never mind that you'd probably be better off using NSURLConnection instead to give you control over caching and such. – Jim Puls Mar 13 '13 at 04:27
  • Hopefully Pratik's answer helps you, but the problems with the above code include 1. you're not checking to make sure that by the time the image has finished loading that the cell hasn't scrolled off and been reused for another row in the table; and 2. if you don't initialize the cell image with a placeholder before to start the async retrieval, you'll see the image from the previous row that this cell was used for. For performance reasons, you'd also want to cache your images (to RAM and persistent storage). Probably easiest to use a proven third-party library. – Rob Mar 13 '13 at 06:22

1 Answers1

6

You can use "AsyncImageView" class files it will load image synchronously and it shows the activity indicator while image loading

AsyncImageView is the class file in which it will create connection for each call and when image data downloading completed it will return image for imageview. and if image is already in cache then just return image without creating connection.

You can download "AsyncImageView" class files from following link:- https://www.dropbox.com/s/peazwjvky9fsjd7/Archive.zip

in .m file import AsyncImageView Class

  #import "AsyncImageView.h" 

in your tableview cell at indexpath method

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"SimpleTableCell";
     UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

     UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(X, y, width, height)];

     NSString *imageURL = [NSString stringWithFormat: @"www.xyz.image.png"];
     AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    [async loadImageFromURL:[NSURL URLWithString:imageURL]];
    [imageView addSubview:async];
    [cell addSubview:imageView];
    return cell;
}

try this your problem will solve.

Pratik
  • 2,399
  • 17
  • 36
  • That's all fine and dandy, but can you explain the code you just pasted in here, or are you just linking to some code that will eventually go dead and help nobody? – CodaFi Mar 13 '13 at 04:29
  • AsyncImageView is the class file in which it will create connection for each call and when image data downloading completed it will return image for imageview. and if image is already in cache then just return image without creating connection – Pratik Mar 13 '13 at 04:34
  • update your answer with that. Explanations are the difference between no-votes and up-votes here. – CodaFi Mar 13 '13 at 04:37