Iphone 目标C线程,在iOS tableViewCell中设置图像

Iphone 目标C线程,在iOS tableViewCell中设置图像,iphone,objective-c,ios,tabs,Iphone,Objective C,Ios,Tabs,我刚刚开始使用线程,我正试图从我的应用程序中的TableView获得更好的性能。每个tableViewCell都有一个imageView,并且在创建tableViewCell时从磁盘加载图像。我想在不同的线程上加载图像,然后在主线程上设置UIImageView。我的问题是,在另一个线程上运行的方法能否向主线程返回值?有没有更好的方法 提前感谢您的帮助。试试以下方法: 是的,您可以将数据传递到主线程。请参阅中的以下方法: 您可以在arg参数中传递一个对象引用。假设您的图标位于文档目录中,则可能是这

我刚刚开始使用线程,我正试图从我的应用程序中的TableView获得更好的性能。每个tableViewCell都有一个imageView,并且在创建tableViewCell时从磁盘加载图像。我想在不同的线程上加载图像,然后在主线程上设置UIImageView。我的问题是,在另一个线程上运行的方法能否向主线程返回值?有没有更好的方法

提前感谢您的帮助。

试试以下方法:
是的,您可以将数据传递到主线程。请参阅中的以下方法:


您可以在
arg
参数中传递一个对象引用。

假设您的图标位于文档目录中,则可能是这样的:

#define DOCUMENTS_DIRECTORY [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

//inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:indexPath, @"indexPath", @"image1.png", @"imageName", nil];
[NSThread detachNewThreadSelector:@selector(loadIcon:) toTarget:self withObject:d];
//

- (void)iconLoaded:(NSDictionary*)dict {
    [icons replaceObjectAtIndex:[[dict objectForKey:@"index"] intValue] withObject:[UIImage imageWithData:[dict objectForKey:@"imageData"]]];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[dict objectForKey:@"indexPath"]]];
}
- (void)loadIcon:(NSDictionary*)dict {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *filePath = [DOCUMENTS_DIRECTORY stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", [dict objectForKey:@"imageName"]]];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    [self performSelectorOnMainThread:@selector(iconLoaded:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:[dict objectForKey:@"indexPath"], "indexPath", imageData, @"imageData", nil] waitUntilDone:YES];


    [pool drain];
}
您需要跟踪正在加载图像的单元格,以便在图像已加载时不要尝试加载图像。可能有一些小的语法错误,因为我没有编译这个,只是徒手写的


icons
是一个
NSMutableArray
为每个单元格保存一个UIImage

这正是我想要的,非常感谢!!我从未想过从另一个线程调用主线程上的方法。无论何时处理UI,都应该始终在主线程中。这就是为什么我没有在
loadIcon:(NSDictionary*)dict
函数中创建UIImage,我只是创建
NSData
并将其传递给UI线程
#define DOCUMENTS_DIRECTORY [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

//inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:indexPath, @"indexPath", @"image1.png", @"imageName", nil];
[NSThread detachNewThreadSelector:@selector(loadIcon:) toTarget:self withObject:d];
//

- (void)iconLoaded:(NSDictionary*)dict {
    [icons replaceObjectAtIndex:[[dict objectForKey:@"index"] intValue] withObject:[UIImage imageWithData:[dict objectForKey:@"imageData"]]];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[dict objectForKey:@"indexPath"]]];
}
- (void)loadIcon:(NSDictionary*)dict {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *filePath = [DOCUMENTS_DIRECTORY stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", [dict objectForKey:@"imageName"]]];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    [self performSelectorOnMainThread:@selector(iconLoaded:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:[dict objectForKey:@"indexPath"], "indexPath", imageData, @"imageData", nil] waitUntilDone:YES];


    [pool drain];
}