2010-07-09 86 views
2

我開發了一個RSS應用程序。在我的表格單元格中,我顯示的圖像是從RSS feed和Title中的圖像鏈接中檢索的。 圖像已成功加載,但問題是它掛起我的應用程序。 有沒有辦法在後臺加載圖片。 我現在的代碼是。如何在iphone應用程序中的UITableCell中加載圖像

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]; 

任何想法請,在此先感謝...

+0

[在UITableViewCell中延遲加載圖片](http://stackoverflow.com/questions/531482/lazy-load-images-in-uitableviewcell)的可能重複 – progrmr 2010-07-09 13:59:05

回答

1

那麼傢伙,我已經做了它的代碼是...

- (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]; 

}

我會打電話給上面的方法在我的代碼...

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

感謝deanWombourne誰給了我這個代碼....

+0

請做一個搜索下一次* *前您發佈一個問題。對於那些試圖提供幫助的人來說,如果我們發佈了答案,並且您只是在做了簡單搜索之後在其他地方獲取了代碼,那就是浪費精力。 – iwasrobbed 2010-07-09 20:02:46

+0

感謝上面的代碼...保存我的日子 – kingston 2011-03-21 11:34:07

-2

你可以這樣使用:

((UIImageView *)cell.backgroundView).image = bgImage; 
((UIImageView *)cell.selectedBackgroundView).image = bgImage; 
+0

你還沒有回答這個問題。問題是如何在後臺加載圖像,而不是設置背景圖像。 – 2014-03-10 09:47:54

相關問題