Objective C 线程,在 iOS tableViewCell 中设置图像

发布于 2024-10-25 18:25:21 字数 205 浏览 1 评论 0原文

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

感谢您提前提供的任何帮助。

I've just started using threads and I'm trying to get better performance from my TableView in my app. Each tableViewCell has an imageView and the image is loaded from disk when the tableViewCell is created. I want to load the Image on a differant thread, then set the UIImageView on the main thread. My question is, can a method that is being ran on another thread return a value to the main thread? Is there a better approach for doin this?

Thanks for any help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏天碎花小短裙 2024-11-01 18:25:21

也许是这样的,假设您的图标位于文档的目录中:

#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

maybe something like this, assuming your icons are in the document's directory:

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

you will need to keep track of which cells you are loading an image for, so you dont try to load one while it is already loading it. there may be some small syntax errors as i did not compile this, just wrote it freehand.

icons is an NSMutableArray holding a UIImage for each cell

∞梦里开花 2024-11-01 18:25:21

是的,您可以将数据传递到主线程。请参阅 NSObject API 文档

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

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

Yes, you can pass data to the main thread. See the following method in NSObject API docs:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

You get to pass a single object reference in the arg parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文