如何制作一个简单的多线程?

发布于 2024-11-17 02:12:02 字数 451 浏览 0 评论 0原文

好的,现在我正在制作一个像餐厅查找器这样的移动应用程序,我想显示餐厅的照片

示例: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 技术交流群。

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

发布评论

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

评论(3

上课铃就是安魂曲 2024-11-24 02:12:02
 if (ImageToDisplay != nil) {
  [self performSelectorInBackground:@selector(loadImage:) object:ImageToDisplay];
}

- (void)loadImage:(ImageToDisplay *)image { //Background method
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData * imageData = [[[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: ImageToDisplay.URL]]autorelease];
    [self performSelectorOnMainThread:@selector(setImageForRestaurant:) withObject:imageData waitUntilDone:NO];
    [pool release];
}

- (void)setImageForRestaurant:(NSData *)imageData { //Change UI in main thread
    ImageForRestaurant.image = [UIImage imageWithData: imageData];
}

我刚刚介绍了多线程的基础知识;我想这会达到你的目的

 if (ImageToDisplay != nil) {
  [self performSelectorInBackground:@selector(loadImage:) object:ImageToDisplay];
}

- (void)loadImage:(ImageToDisplay *)image { //Background method
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData * imageData = [[[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: ImageToDisplay.URL]]autorelease];
    [self performSelectorOnMainThread:@selector(setImageForRestaurant:) withObject:imageData waitUntilDone:NO];
    [pool release];
}

- (void)setImageForRestaurant:(NSData *)imageData { //Change UI in main thread
    ImageForRestaurant.image = [UIImage imageWithData: imageData];
}

I've just included basics in multi-threading; I guess it will serve your purpose

权谋诡计 2024-11-24 02:12:02

您是否尝试过

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

我认为这是最简单的方法。

Did you try

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

I think it's the easiest way.

污味仙女 2024-11-24 02:12:02

使用 ASIHTTPRequest 库。请参阅本页上的“创建异步请求”:http:// /allseeing-i.com/ASIHTTPRequest/How-to-use

还有新库 AFNetworking看起来很有希望。正如他们所说:

如果您厌倦了试图做太多事情的大型库,如果您已经开始自己推出自己的黑客解决方案,如果您想要一个真正使 iOS 网络代码变得有趣的库,请尝试 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:

If you're tired of massive libraries that try to do too much, if you've taken it upon yourself to roll your own hacky solution, if you want a library that actually makes iOS networking code kinda fun, try out AFNetworking.

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