2012-08-14 93 views
0

目前我正在聊天的應用程序,我試圖上傳圖像,每一件事情都很好,除非當圖像上傳UI凍結,所以異步方法進入現場,這就是我努力做到:dispatch_async衝突

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 

    [self dismissModalViewControllerAnimated:YES]; 
     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul); 
     dispatch_async(queue, ^{ 

     UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage]; 
     NSData *imgData = UIImageJPEGRepresentation(image, 1.0); 

     //[self performSelectorOnMainThread:@selector(send:) withObject:imgData waitUntilDone:YES]; 

     [self send:imgData]; 
}); 

} 

我收到此錯誤:

Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

  1. WebThreadLock
  2. -[UITextView setText:]
  3. -[HPTextViewInternal setText:]
  4. -[HPGrowingTextView setText:]
  5. -[chatViewController send:]
  6. __74-[chatViewController imagePickerController:didFinishPickingMediaWithInfo:]_block_invoke_0
  7. _dispatch_call_block_and_release
  8. _dispatch_worker_thread2
  9. _pthread_wqthread
  10. start_wqthread

我使用HPGrowingTextView給予的iMessage一種可擴展的打字區的打字消息,但得到這個問題。

我搜索這個錯誤

Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread

人民建議使用performSelectorOnMainThread但這種做法再次凍結UI。

如何解決這個衝突或是否有其他方法。

Inside [self send:imageData] 
...building a url and appending hFile(imageData) 
[body appendData:[NSData dataWithData:hFile]]; 
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
      // setting the body of the post to the reqeust 
      [request setHTTPBody:body]; 

      NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
      NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

      NSString *imgUrl = [NSString stringWithFormat:@"http://www.www.www/uImages/thumbs/%@",returnString]; 
... 

上傳後,圖像的縮略圖返回,如果我使用[NSURLConnection sendAsynchronousRequest我得到我在我的UITableView空diplaying縮略圖。

+0

什麼是[self send:imgData];呢? – J2theC 2012-08-14 18:59:07

+0

感謝您的回覆,它的圖片上傳方法.. – 2012-08-14 19:00:36

+0

那麼你是100%確定調用從主線程以外的線程調用UI,也許你有一個警告視圖彈出,如果文件上傳好嗎?這是不好的,你應該在主線程上執行那樣的東西 – Daniel 2012-08-14 19:01:12

回答

1

當你想改變UI中的任何東西時,你應該在主線程上做。

所以,如果你想改變你有HPGrowingTextView控制的文字,你可以做到以下幾點:

dispatch_async(dispatch_get_main_queue(), ^{ 
    growingTextView.text = @"Some text"; 
}) 
+0

但我不改變growTextView文本。 – 2012-08-14 19:22:14

+0

也許你沒有明確地改變它,但'[HPGrowingTextView setText:]'方法肯定是在方法'[chatViewController send:]'內部的某處調用的。 – Hejazi 2012-08-14 19:26:56

+0

對不起,我忘記了,其實我正在改變文字growingtextview.text = @「」這是爲了清除它,但現在在圖像上傳時,當我嘗試鍵入內容時,它又崩潰了,打字必須做一些與主或第二個線程? – 2012-08-14 19:29:33

0

你所得到的崩潰,因爲您呼叫發送主線程之外。堆棧跟蹤對於這個事實是顯而易見的。

您需要在主線程上進行這些調用。當你這樣做,但是,你的UI當然會掛起,因爲這個調用...

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

從方法的名稱,很明顯,這個調用是同步的,將阻止,直到返回結果。

因此,您需要改爲使用異步表單。

sendAsynchronousRequest:queue:completionHandler: