如何在iphone应用程序中的UITableCell中加载图像

问题描述:

我开发了一个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://*.com/questions/531482/lazy-load-images-in-uitableviewcell)的可能重复 – progrmr 2010-07-09 13:59:05

转播......你也应该看看懒加载表通过苹果的示例代码http://developer.apple.com/iphone/library/samplecode/LazyTableImages/Introduction/Intro.html

更新另一个例子在这里:http://www.markj.net/iphone-asynchronous-table-image/

并且从Apple看看这个文件。

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

大多数我用NSURLConnection下载东西的时候。

那么家伙,我已经做了它的代码是...

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

你可以这样使用:

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

你还没有回答这个问题。问题是如何在后台加载图像,而不是设置背景图像。 – 2014-03-10 09:47:54