Search code examples
iosswiftuiimageviewios9

Elusive hard crash from UIImageView 'unrecognized selector sent to instance'


I've been getting the following exception when programmatically starting a new storyboard from the main app screen, ONLY after 2+ new photos have been saved into phone/simulator's Camera Roll (outside the app - camera/save image to camera roll).

The weirdest part is that the code that actually accesses the Camera Roll is not executed until a future view of that storyboard (so that's not where the problem is).

I've narrowed it down to be coming from some UIImageView that has been previously loaded on the main screen, but it's puzzling why this would happen ONLY when Camera Roll gets new photos.

-[__NSCFType animationForKey:]: unrecognized selector sent to instance 0x600000827a00

>     0   CoreFoundation                      0x000000010ad9dd4b __exceptionPreprocess + 171
>     1   libobjc.A.dylib                     0x000000010a80621e objc_exception_throw + 48
>     2   CoreFoundation                      0x000000010ae0df04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
>     3   CoreFoundation                      0x000000010ad23005 ___forwarding___ + 1013
>     4   CoreFoundation                      0x000000010ad22b88 _CF_forwarding_prep_0 + 120
>     5   UIKit                               0x0000000108a9dc11 -[UIImageView isAnimating] + 156
>     6   UIKit                               0x0000000108a9db25 -[UIImageView stopAnimating] + 116
>     7   UIKit                               0x0000000108a9b46b -[UIImageView dealloc] + 51
>     8   CoreFoundation                      0x000000010adf525e common_removeAllObjects + 254
>     9   CoreFoundation                      0x000000010acd1c93 -[__NSArrayM dealloc] + 19
>     10  libobjc.A.dylib                     0x000000010a81ab12 _ZN11objc_object17sidetable_releaseEb + 212
>     11  libobjc.A.dylib                     0x000000010a81b1d1 _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 715
>     12  QuartzCore                          0x00000001083b7371 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 369
>     13  QuartzCore                          0x00000001083e43ff _ZN2CA11Transaction6commitEv + 475
>     14  UIKit                               0x0000000108883d9b _UIApplicationFlushRunLoopCATransactionIfTooLate + 206
>     15  UIKit                               0x000000010908e77c __handleEventQueue + 5672
>     16  CoreFoundation                      0x000000010ad42761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
>     17  CoreFoundation                      0x000000010ad2798c __CFRunLoopDoSources0 + 556
>     18  CoreFoundation                      0x000000010ad26e76 __CFRunLoopRun + 918
>     19  CoreFoundation                      0x000000010ad26884 CFRunLoopRunSpecific + 420
>     20  GraphicsServices                    0x000000010d07ea6f GSEventRunModal + 161
>     21  UIKit                               0x000000010888ac68 UIApplicationMain + 159
>     22  MyApp                               0x0000000104f652b2 main + 114
>     23  libdyld.dylib                       0x000000010bbd568d start + 1
>     24  ???                                 0x0000000000000001 0x0 + 1

Solution

  • After progressively commenting out the code that could potentially be causing this crash, I've narrowed it down to the following code that hides the Status Bar within the instantiated storyboard

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    

    My guess is that the hiding of the Status Bar forces main screen views to re-layout, which leads to that exception with one of the UIImageViews. So, I've decided to stop hiding the Status Bar for now since it's not essential for the app. Hopefully this helps someone seeing the same exception.

    override func prefersStatusBarHidden() -> Bool {
         return **false**
    }