views:

74

answers:

4

Hi 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...

+2  A: 

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
A: 

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
A: 

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
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
A: 

You can use this way:

((UIImageView *)cell.backgroundView).image = bgImage;
((UIImageView *)cell.selectedBackgroundView).image = bgImage;
Son Nguyen