如何制作一个简单的多线程?
好的,现在我正在制作一个像餐厅查找器这样的移动应用程序,我想显示餐厅的照片
示例:restaurant x
这是一个代码:
if (ImageToDisplay != nil)
{
NSData * imageData = [[[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: ImageToDisplay.URL]]autorelease];
ImageForRestaurant.image = [UIImage imageWithData: imageData];
}
问题是这个下载过程图片可能需要太长时间。所以我希望该进程在不同的线程上运行。
这样之后的代码就可以运行,而不必等待这一代码完成。
我怎样才能这样做呢?
Okay, now I was making a mobile application like restaurant finder, I want to show a photo of the restaurant
the example : restaurant x <image x>
It's a code:
if (ImageToDisplay != nil)
{
NSData * imageData = [[[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: ImageToDisplay.URL]]autorelease];
ImageForRestaurant.image = [UIImage imageWithData: imageData];
}
The problem is this process of downloading pictures may take too long. So I want the process to be run on a different thread.
That way the code after that can run without having to wait this one to finish.
How can I do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚介绍了多线程的基础知识;我想这会达到你的目的
I've just included basics in multi-threading; I guess it will serve your purpose
您是否尝试过
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
我认为这是最简单的方法。
Did you try
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
I think it's the easiest way.
使用 ASIHTTPRequest 库。请参阅本页上的“创建异步请求”:http:// /allseeing-i.com/ASIHTTPRequest/How-to-use
还有新库 AFNetworking看起来很有希望。正如他们所说:
Use the ASIHTTPRequest library. See 'Creating an asynchronous request' on this page: http://allseeing-i.com/ASIHTTPRequest/How-to-use
Also the new library AFNetworking looks promising. As they say: