首页 文章

从网址下载图片,记录错误

提问于
浏览
0

当我从URL下载图像时,它需要大约6秒钟(即使图像不是高清)并且在日志中出错 .

这是我下载图片的代码:

let backgroundQueue = DispatchQueue(label: "my.bk.job", qos: .background, target: nil)

    backgroundQueue.async {
        let url = URL(string: "https://url/img.jpg")
        let data = try? Data(contentsOf: url!)
        //Show the image
    }

这是我得到的日志错误:

2016-12-18 16:16:17.243477 yolo [1111:33384] [] nw_host_stats_add_src recv太小,收到24,预计28 2016-12-18 16:16:17.247960 yolo [1111:33384] [] __nwlog_err_simulate_crash模拟崩溃已模拟“nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK失败:[42]协议不可用”2016-12-18 16:16:17.248434 yolo [1111:33384] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK失败:[42]协议不可用,转储回溯:[x86_64 ] libnetcore-856.30.16 0 libsystem_network.dylib 0x000000010c1b7666 __nw_create_backtrace_string 123 1 libnetwork.dylib 0x000000010c495006 nw_socket_add_input_handler 3164 2 libnetwork.dylib 0x000000010c472555 nw_endpoint_flow_attach_protocols 3768 3 libnetwork.dylib 0x000000010c471572 nw_endpoint_flow_setup_socket 563 4 libnetwork.dylib 0x000000010c470298 - [NWConcrete_nw_endpoint_flow startWithHandler:] 2612 5 libnetwork.dylib 0x000000010c48bae1 nw_endpoint_handler_path_change 1261 6 libnetwork.dylib 0x 000000010c48b510 nw_endpoint_handler_start 570 7 libnetwork.dylib 0x000000010c4a31f9 nw_endpoint_resolver_start_next_child 2240 8 libdispatch.dylib 0x000000010bf34978 _dispatch_call_block_and_release 12 9 libdispatch.dylib 0x000000010bf5e0cd _dispatch_client_callout 8 10 libdispatch.dylib 0x000000010bf3be17 _dispatch_queue_serial_drain 236 11 libdispatch.dylib 0x000000010bf3cb4b _dispatch_queue_invoke 1073 12 libdispatch.dylib 0x000000010bf3f385 _dispatch_root_queue_drain 720 13 libdispatch.dylib 0x000000010bf3f059 _dispatch_worker_thread3 123 14 libsystem_pthread.dylib 0x000000010c3074de _pthread_wqthread 1129 15 libsystem_pthread.dylib 0x000000010c305341 start_wqthread 13

The code work because I can see the Image but is slow and this error don't seem good to me, What I'm doing wrong? (我正在使用Swift 3)

编辑:

我尝试了一个白色的新项目中的代码,几乎没有任何东西,仍然收到它 .

我也复制/粘贴了this代码但得到了同样的错误 .

我更改了URL,现在下载时间正确,所以我将保留此错误,我将使用此代码 .

谢谢大家的帮助,我提出了所有有用的答案

3 回答

  • 0

    你必须在主队列上做UI东西:

    backgroundQueue.async {
        let url = URL(string: "https://url/img.jpg")
        let data = try? Data(contentsOf: url!)
        DispatchQueue.main.async {
            //Show the image
        }
    }
    
  • 3

    这些无用的日志始终显示在XCode 8上 . 如果您不想看到它们,请在XCode Edit Scheme - > Arguments 上设置环境变量 OS_ACTIVITY_MODE = Disable .

    原则:

    1.使用异步下载任何文件或图像通过子线程2.下载后,通过同步主线程将图像更新到UI

    这很容易解决 .

  • 2

    错误/警告可能与您的代码无关 .

    有关SO_NOAPNFALLBK消息,请参阅here,其余消息请参见here .

相关问题