首页 > 解决方案 > 应用程序在从库中选择媒体时崩溃,但将 nil 元素插入数组时除外

问题描述

我有一个旧应用程序,我的公司仍然为其提供旧版支持。

我们的一些客户发生了一个奇怪的错误,但我们这边没有。当用户从 ImagePickerController 中选择一些图像时,显然会发生这种情况。

例外

异常说:

致命异常:NSInvalidArgumentException

-[PSImageViewController imagePickerController:didFinishPickingMediaWithInfo:] -[__NSPlaceholderArray initWithObjects:count:]: 尝试从对象[0]中插入零对象

堆栈跟踪

堆栈跟踪是:

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x2017b7ea0 __exceptionPreprocess
1  libobjc.A.dylib                0x200989a40 objc_exception_throw
2  CoreFoundation                 0x20172f470 _CFArgv
3  CoreFoundation                 0x2016a85f4 -[__NSPlaceholderArray initWithObjects:count:]
4  CoreFoundation                 0x2017b3600 __createArray
5  CoreFoundation                 0x2016b0584 +[NSArray arrayWithObject:]
6  MyAppName             0x102191378 -[PSImageViewController imagePickerController:didFinishPickingMediaWithInfo:] (PSImageViewController.m:221)
7  UIKitCore                      0x22e30e30c -[UIImagePickerController _imagePickerDidCompleteWithInfo:]
8  PhotoLibrary                   0x214add528 (Missing)
9  CameraUI                       0x21f38c728 -[CAMImagePickerCameraViewController _handleCapturedImagePickerPhotoWithCropOverlayOutput:]
10 AssetsLibraryServices          0x20f173814 __pl_dispatch_sync_block_invoke
11 libdispatch.dylib              0x2011f2484 _dispatch_client_callout
12 libdispatch.dylib              0x20119f6b0 _dispatch_async_and_wait_invoke
13 libdispatch.dylib              0x2011f2484 _dispatch_client_callout
14 libdispatch.dylib              0x20119e9b4 _dispatch_main_queue_callback_4CF$VARIANT$mp
15 CoreFoundation                 0x201747dd0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
16 CoreFoundation                 0x201742c98 __CFRunLoopRun
17 CoreFoundation                 0x2017421cc CFRunLoopRunSpecific
18 GraphicsServices               0x2039b9584 GSEventRunModal
19 UIKitCore                      0x22e985054 UIApplicationMain
20 MyAppName             0x1020d60fc main (main.m:17)
21 libdyld.dylib                  0x201202bb4 start

代码

我的 ImagePickerController 委托代码是:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if([info objectForKey:UIImagePickerControllerEditedImage])
    {
        self.image = [info valueForKey:UIImagePickerControllerEditedImage];
    }
    else
    {
        self.image = [info valueForKey:UIImagePickerControllerOriginalImage];
    }
    [self.imgProperty setImage:self.image];
     [[PSDataPersistenceManager sharedManager] updateModificationStatusOfAppointments:[NSArray arrayWithObject:self.propertyObj.propertyToAppointment] toModificationStatus:[NSNumber numberWithBool:YES]];
    [self dismissViewControllerAnimated:YES completion:nil];

}

如您所见,我实际上并没有在这里做任何可能导致该异常的事情。打开 imagePicker 的代码是:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    CLS_LOG(@"Clicked Index %d",buttonIndex);

    if (actionSheet.tag == 1)
    {
        if (buttonIndex != actionSheet.cancelButtonIndex)
        {
            if (buttonIndex == actionSheet.firstOtherButtonIndex)
            {
                [self showCameraOfSourceType:UIImagePickerControllerSourceTypeCamera];

            }
            else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1)
            {
                [self showCameraOfSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; return;
            }

        }
    }
}

trace 中的具体行PhotoLibrary 0x214add528 (Missing)很好奇。我认为不知何故用户没有 PhotoLibrary,或者为 Imagepicker 选择了错误的媒体源。或者是

[[PSDataPersistenceManager sharedManager] updateModificationStatusOfAppointments:[NSArray arrayWithObject:self.propertyObj.propertyToAppointment] toModificationStatus:[NSNumber numberWithBool:YES]];

哪个导致以self.propertyObj.propertyToAppointment某种方式为空的问题?我查过了,不能为空。否则此屏幕上会出现更多崩溃。它在我的测试运行中始终有效。

我该如何解决这个崩溃?

标签: iosobjective-cinvalidargumentexception

解决方案


错误来自第 221 行。您使用以下命令创建数组:

[NSArray arrayWithObject:self.propertyObj.propertyToAppointment]

错误告诉你那self.propertyObj.propertyToAppointmentnil.


推荐阅读