使用数据

时间:2018-01-24 10:51:12

标签: ios multithreading uiimage

从服务器获取图像时,我在以下代码中收到了一些崩溃报告:

DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {

    if
        let imageData = try? imageFile.getData(), // This gets the image data from the server
        let image = UIImage(data: imageData) { // This is where the crash seems to happen
        //...
    }
}

崩溃报告:

Thread 3 Crashed:
0   ImageIO                              0x0000000184c3bef0 ERROR_ImageIO_DataIsNotReadable(unsigned char*) + 16
1   ImageIO                              0x0000000184c3bd90 IIOImageSource::doBindToReader() + 280
2   ImageIO                              0x0000000184c3c180 IIOImageSource::updatedCount() + 48
3   ImageIO                              0x0000000184c40040 CGImageSourceGetCount + 152
4   UIKit                                0x000000018c32c29c _UIImageRefFromData + 396
5   UIKit                                0x000000018c49d608 -[UIImage(UIImagePrivate) _initWithData:preserveScale:cache:] + 120
6   MyApp                               0x0000000100fd60d0 function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed> of closure #1 () -> () in closure #1 (Any?, Swift.Error?) -> () in MyApp.MyClass.myFunction(forUser: MyApp.User) -> () (MyClass.swift:0)
7   MyApp                               0x0000000100fdba10 partial apply forwarder for closure #1 () -> () in closure #1 (Any?, Swift.Error?) -> () in MyApp.MyClass.myFunction(forUser: MyApp.User) -> () (MyClass.swift:0)
8   MyApp                               0x00000001010a90a4 reabstraction thunk helper from @callee_owned () -> () to @callee_unowned @convention(block) () -> () (MyController.swift:0)
9   libdispatch.dylib                    0x0000000182692a54 _dispatch_call_block_and_release + 20
10  libdispatch.dylib                    0x0000000182692a14 _dispatch_client_callout + 12
11  libdispatch.dylib                    0x000000018269fea4 _dispatch_root_queue_drain + 1028
12  libdispatch.dylib                    0x000000018269fa38 _dispatch_worker_thread3 + 116
13  libsystem_pthread.dylib              0x000000018293b06c _pthread_wqthread + 1264
14  libsystem_pthread.dylib              0x000000018293ab6c start_wqthread + 0

我无法重现崩溃,但我有两个猜测:

  • UIImage(data:)在后​​台线程上执行。目前正在讨论UIImage UIKit是否是线程安全的。但是Apple docs现在更清楚地表明UIImage是线程安全的:

      

    “图像对象的不可变性也意味着它们是安全的   从任何线程创建和使用。“

  • 图像数据损坏的方式导致未在UIImage创建中捕获的异常。并且因为在许多图像中只有少数数据有损坏的数据我无法重现崩溃。在尝试从中创建UIImage之前,我可能需要手动执行数据测试。

任何人都可以解释崩溃报告吗?

0 个答案:

没有答案