目前我正在聊天的應用程序,我試圖上傳圖像,每一件事情都很好,除非當圖像上傳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...
- WebThreadLock
- -[UITextView setText:]
- -[HPTextViewInternal setText:]
- -[HPGrowingTextView setText:]
- -[chatViewController send:]
- __74-[chatViewController imagePickerController:didFinishPickingMediaWithInfo:]_block_invoke_0
- _dispatch_call_block_and_release
- _dispatch_worker_thread2
- _pthread_wqthread
- 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縮略圖。
什麼是[self send:imgData];呢? – J2theC 2012-08-14 18:59:07
感謝您的回覆,它的圖片上傳方法.. – 2012-08-14 19:00:36
那麼你是100%確定調用從主線程以外的線程調用UI,也許你有一個警告視圖彈出,如果文件上傳好嗎?這是不好的,你應該在主線程上執行那樣的東西 – Daniel 2012-08-14 19:01:12