2

I have develop an RSS application. in My table cell I am displaying images which i retrive from imge link in RSS feed and Title. Images are loaded successfully but the problem is that it hang my application. as there any way to load image in background. my current code is.

int blogEntryIndex1 = [indexPath indexAtPosition: [indexPath length] -1];
        imgstring=[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: @"image"];
NSURL *url = [NSURL URLWithString:imgstring];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:data];
     cell.imageView.image=[img autorelease];

any idea please, Thanks in advance...

Nauman.Khattak
  • 641
  • 4
  • 12
  • 33
  • possible duplicate of [Lazy load images in UITableViewCell](http://stackoverflow.com/questions/531482/lazy-load-images-in-uitableviewcell) – progrmr Jul 09 '10 at 13:59

4 Answers4

4

Repost... You should also look at lazy table loading via Apple's sample code http://developer.apple.com/iphone/library/samplecode/LazyTableImages/Introduction/Intro.html

Update another example here: http://www.markj.net/iphone-asynchronous-table-image/

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
1

Well guys i have done it the code is...

- (void) loadImage:(id)object {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Get the data to pass in
NSDictionary *dict = (NSDictionary *)object;

// Get the cell and the image string
NSString *imgstring = [dict objectForKey:@"imgstring"];
MyfirstCell *cell = [dict objectForKey:@"cell"];

NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
    cell.myimnage.image = img;
[pool release];

}

I will call the above method in my code...

if(searching) {
        //cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
        int blogEntryIndex = [indexPath indexAtPosition: [indexPath length] -1];
        // Tell your code to load the image for a cell in the background
        NSString *imgstring =[[blogEntries objectAtIndex: blogEntryIndex] objectForKey: @"image"];
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:cell, @"cell", imgstring, @"imgstring", nil];
        [self performSelectorInBackground:@selector(loadImage:) withObject:dict];
        //background code end here..

Thanks to deanWombourne who has given me this code....

Nauman.Khattak
  • 641
  • 4
  • 12
  • 33
  • Please do a search next time *before* you post a question. For those trying to help, it's a wasted effort if we post answers and you just grab the code elsewhere after doing a simple search. – iwasrobbed Jul 09 '10 at 20:02
0

And take a look to this document also from Apple.

http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html

Most the time I use NSURLConnection to download things.

Sandro Meier
  • 3,071
  • 2
  • 32
  • 47
-2

You can use this way:

((UIImageView *)cell.backgroundView).image = bgImage;
((UIImageView *)cell.selectedBackgroundView).image = bgImage;
Son Nguyen
  • 3,481
  • 4
  • 33
  • 47
  • You have not answered the question. The question was how to load the image in the background, not set the background image. – Johan Karlsson Mar 10 '14 at 09:47