0

I am having problem with the loading of content in the app, I see that the data is fetched by the app but the images take lot of time to load, is there any possibility to load images afterwords. The code is below:

NSDictionary *dict=[discussionArray objectAtIndex:indexPath.row];
UIImageView *avatarimage = (UIImageView *)[cell viewWithTag:4];
NSString *photoStrn=[dict objectForKey:@"photo"];

 dispatch_async(dispatch_get_global_queue(0,0), ^{
                   NSString *u=[NSString stringWithFormat:@"http://%@",photoStrn];
                   NSURL *imageURL=[NSURL URLWithString:u];
                   NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
                   dispatch_sync(dispatch_get_main_queue(), ^{
                   UIImage *dpImage = [UIImage imageWithData:imageData];
                   if (dpImage==nil)
                   {
                     dpImage = [UIImage imageNamed:@"profileImage.png"];
                   }
                   avatarimage.image = dpImage;

        });

If you want more details I will provide :)

Till
  • 27,559
  • 13
  • 88
  • 122
James Mitchee
  • 143
  • 5
  • 16

3 Answers3

4

You can use GCD for doing this:

dispatch_async(dispatch_get_global_queue(0,0), ^{
     for (NSDictionary *dic in discussionArray)
     {
        NSString *photoStr=[dic objectForKey:@"photo"];
        NSString * photoString=[NSString stringWithFormat:@"http://%@",photoStr];
        UIImage *dpImage =  [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]];
        if (dpImage==nil)
        {
            dpImage = [UIImage imageNamed:@"profileImage.png"];
        }
     }
});
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • I tried this approach the pages are loading fast :), though there is one problem that is on scrolling the app changes avatar images and reloads the images. I think I have made some error, I am updating the code above. – James Mitchee Mar 14 '13 at 20:35
  • @JamesMitchee I guess that you are using a tableView and that you forgot the reset the image back to its default when reusing a cell. – Till Mar 14 '13 at 20:38
0

Get SDWebImage Here and add that in your project and include

UIImageView+WebCache.h

in class implementation file

UIImageView *imag=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[imag setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[jsonarray objectAtIndex:indexPath.row]valueForKey:@"imgurl"]]] placeholderImage:[UIImage imageNamed:@"Icon@2x.png"]];
[self.view addSubview:imag];
[imag release];

SDWebImage will be more useful for loading images from URL.

Hope this Helps !!!

Vishnu
  • 2,243
  • 2
  • 21
  • 44
0

James,

With this line,

[NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]]

you make a synchronous network call on the main thread. The current thread will hang untile the network call is complete.

The solution would be to do an asynchronous network call. The AFNetworking library provides a great category to load images asynchronously : UIImageView+AFNetworking.