我如何运行一个没有阻止我的iPhone应用程序中的用户界面的进程

我正在访问iPhone上的照片库,它需要很长时间导入我select在我的应用程序中的图片,我如何在辅助线程上运行该过程,或者我用什么解决scheme不阻止用户界面?

我在这里使用了performSelectOnBackground或GCD对示例代码做了完整的解释:

GCD,线程,程序stream和UI更新

以下是该post的示例代码部分(减去他的具体问题:

performSelectorInBackground示例:

在这个片段中,我有一个调用长时间运行的工作的button,一个状态标签,我添加了一个滑块来显示我可以在bg工作完成时移动滑块。

// on click of button - (IBAction)doWork:(id)sender { [[self feedbackLabel] setText:@"Working ..."]; [[self doWorkButton] setEnabled:NO]; [self performSelectorInBackground:@selector(performLongRunningWork:) withObject:nil]; } - (void)performLongRunningWork:(id)obj { // simulate 5 seconds of work // I added a slider to the form - I can slide it back and forth during the 5 sec. sleep(5); [self performSelectorOnMainThread:@selector(workDone:) withObject:nil waitUntilDone:YES]; } - (void)workDone:(id)obj { [[self feedbackLabel] setText:@"Done ..."]; [[self doWorkButton] setEnabled:YES]; } 

GCD示例:

 // on click of button - (IBAction)doWork:(id)sender { [[self feedbackLabel] setText:@"Working ..."]; [[self doWorkButton] setEnabled:NO]; // async queue for bg work // main queue for updating ui on main thread dispatch_queue_t queue = dispatch_queue_create("com.sample", 0); dispatch_queue_t main = dispatch_get_main_queue(); // do the long running work in bg async queue // within that, call to update UI on main thread. dispatch_async(queue, ^{ [self performLongRunningWork]; dispatch_async(main, ^{ [self workDone]; }); }); } - (void)performLongRunningWork { // simulate 5 seconds of work // I added a slider to the form - I can slide it back and forth during the 5 sec. sleep(5); } - (void)workDone { [[self feedbackLabel] setText:@"Done ..."]; [[self doWorkButton] setEnabled:YES]; } 

使用asynchronous连接。 它不会阻止用户界面,而是在后面进行提取。

当我不得不下载图像时, 这帮助了我很多。

一个选项是使用performSelectorInBackground:withObject: