0

目前我正在开发一个聊天应用程序,我正在尝试上传图像,除了图像上传 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 的结果。现在崩溃...

  1. Web线程锁
  2. -[UITextView 设置文本:]
  3. -[HPTextView 内部设置文本:]
  4. -[HPGrowingTextView 设置文本:]
  5. -[chatViewController 发送:]
  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 类型的可扩展输入区域来输入消息,但是遇到了这个问题。

我搜索了这个错误

试图从除主线程或 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 中显示。

4

2 回答 2

1

当您想更改 UI 中的任何内容时,您应该在主线程上进行。

因此,如果您想更改HPGrowingTextView您拥有的控件文本,您可以执行以下操作:

dispatch_async(dispatch_get_main_queue(), ^{
    growingTextView.text = @"Some text";
})
于 2012-08-14T19:20:15.253 回答
0

您正在崩溃,因为您在主线程之外调用 send 。关于这个事实,堆栈跟踪是显而易见的。

您需要在主线程上进行这些调用。但是,当你这样做时,你的 UI 当然会因为这个调用而挂起......

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

从方法名可以看出,这个调用是同步的,会阻塞直到返回结果。

因此,您需要改用异步形式。

sendAsynchronousRequest:queue:completionHandler:
于 2012-08-14T19:34:09.753 回答