问题描述
有没有办法在iphone异步加载本地文件?我加载使用此为我的UITableView uiimages:
is there way to load local files in iphone asynchronously? I load uiimages for my uitableview using this:
NSData *imageData = [[NSData alloc] initWithContentsOfFile:fileName];
UIImage *cachedImage = [[[UIImage alloc] initWithData:imageData] autorelease];
但它是缓慢的,因为主线程被锁定或东西,直到NSData的完成加载该文件,用户界面变得无法响应。有什么样NSURLConnection的,但对本地文件?所以,我可以只加载文件而不冻结UI,当它完成加载,有些处理程序发送通知或类似的东西。
but it is slow, because main thread is locked or something until NSData finishes loading the file and UI becomes unresponsive. Is there something like NSURLConnection but for local files? So I can just load file without freezing UI, and when it finishes loading, some handler sends notification or something like that.
推荐答案
您可以使用一个NSOperationQueue和NSInvocationOperation调用一个'负荷'的过程。然后,从装载程序,只需使用performSelectorOnMainThread进行更新。请参阅:以一个详细的例子
You can use an NSOperationQueue and NSInvocationOperation to call a 'load' procedure. Then, from the load procedure, simply use the 'performSelectorOnMainThread' to update. See: http://gist.github.com/375559 for a detailed example.
这篇关于iphone异步文件加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!