Error ImageIO: data is not readable

I get some crash reports in the following code when fetching images from a server:


DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {
   if
      let imageData = try? imageFile.getData(),
      let image = UIImage(data: imageData) {
      //...

   }
 }


The crash report:


    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



I am not able to reproduce the crash but I have two guesses:

  • UIImage(data:)
    is executed on a background thread. There are ongoing discussions about whether UIImage being of UIKit is thread safe. However the Apple docs now more clearly suggest that
    UIImage
    is thread safe:
    "The immutable nature of image objects also means that they are safe to create and use from any thread."
  • The image data is corrupted in a way that causes an exception that is not caught within the creation of
    UIImage
    . And because among many images only a few have corrupted data I could not reproduce the crash. Maybe there is a data test that I should do manually before trying to create an
    UIImage
    from it.


Can anyone interpret the crash report?