簡體   English   中英

如何在不阻止我的iPhone應用程序中的用戶界面的情況下運行流程

[英]How do I run a process without blocking user interface in my iphone app

我正在訪問iphone上的照片庫,導入我在我的應用程序中選擇的圖片需要很長時間,如何在輔助線程上運行該進程,或者我使用什么解決方案來阻止用戶界面?

我在這里使用performSelectOnBackground或GCD對示例代碼做了完整的解釋:

GCD,線程,程序流和UI更新

這是該帖子的示例代碼部分(減去他的具體問題:

performSelectorInBackground示例:

在這個片段中,我有一個調用長時間運行工作的按鈕,一個狀態標簽,我添加了一個滑塊,以顯示我可以在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];
}

使用異步連接。 它不會阻止UI在后面進行提取時。

當我不得不下載圖片時, 對我幫助很大。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM