当我在 Xcode 8 中运行我的项目时收到以下错误消息:
TCP_CONNECTION_EVENT_DISCONNECTED in response to state failed and error Error Domain=kNWErrorDomainPOSIX Code=61 "Connection refused" UserInfo={NSDescription=Connection refused}
这是导致这个问题的代码:
@implementation UIImageView (LoadAsyn)
+ (void) loadFromURL: (NSURL*) url callback:(void (^)(UIImage *image))callback {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData * imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
callback(image);
});
});
}
顺便说一句,我检查了我的网址。完全没有问题。有人提到dataWithContentsOfURL
只能用于访问本地资源。但这是苹果所说的:
A data object containing the data from the location specified by aURL. Returns nil if the data object could not be created.
它并没有说我们不能将它用于远程 url,我已经在我以前的 Xcode 中使用它很长时间了。
有什么想法吗?谢谢。
附言:
对不起,我想我可能是错的。基于 Apple 的最新文档:
Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.
但我以异步方式使用它(在主线程以外的线程中)。我有点困惑,我会对此做更多研究。
最佳答案
虽然你从后台线程调用它,但这个方法是与主线程同步的。它不依赖于调用它的线程。
关于ios - Xcode 8 中的 TCP_CONNECTION_EVENT_DISCONNECTED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39738094/