ios - Xcode 8 中的 TCP_CONNECTION_EVENT_DISCONNECTED

标签 ios xcode8

当我在 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/

相关文章:

iphone - 如何在结构中使用 __unsafe_unretained

ios - NavigationBar 标题为黑色

ios - 如何在屏幕顶部附近显示带有触摸的导航栏?

swift - 代码 8 : Cannot inherit class from the framework

iOS 10 相机 View 显示 API_Cancel_Title 而不是取消

ios - Xcode 8.3.3 括号高亮很不一致

ios - 单选按钮组 Swift 3 Xcode 8

ios - 修复静态 tableviewcontroller xcode 8 中 Storyboard 中的所有警告

ios - Firebase pod 安装错误其他 pod 安装

php - 如何为我的 iOS 应用程序设计我的服务器数据库?