目前我正在开发一个聊天应用程序,我正在尝试上传图像,除了图像上传 UI 冻结时,一切都工作正常,所以异步方法进入了场景,这就是我想要做的:
- (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];
});
}
我收到此错误:
试图从除主线程或 web 线程之外的线程获取 web lock。这可能是从辅助线程调用 UIKit 的结果。现在崩溃...
- Web线程锁
- -[UITextView 设置文本:]
- -[HPTextView 内部设置文本:]
- -[HPGrowingTextView 设置文本:]
- -[chatViewController 发送:]
- __74-[chatViewController imagePickerController:didFinishPickingMediaWithInfo:]_block_invoke_0
- _dispatch_call_block_and_release
- _dispatch_worker_thread2
- _pthread_wqthread
- start_wqthread
我正在使用 HPGrowingTextView 提供一种 iMessage 类型的可扩展输入区域来输入消息,但是遇到了这个问题。
我搜索了这个错误
试图从除主线程或 web 线程之外的线程获取 web lock。这可能是从辅助线程调用 UIKit 的结果
人们建议使用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 中显示。