【问题标题】:UIImagePickerController crashes on alloc/initUIImagePickerController 在 alloc/init 上崩溃
【发布时间】:2012-05-22 13:24:45
【问题描述】:

我有一个 UIImagePickerController,当用户按下按钮时,我会在弹出窗口中显示它。这在 iPad 模拟器中完全正常,但是当我尝试在实际测试设备上做同样的事情时,我在图像选择器的 alloc/init 行上得到一个NSRangeException

    imagePicker = [[UIImagePickerController alloc] init];//Crashes here on device
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) (kUTTypeImage), nil];

这是崩溃消息:

* 由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:'* -[NSOrderedSet initWithOrderedSet:range:copyItems:]: range {8, 1} 超出范围 [0 .. 0 ]'

我已通过尝试在调试模式下跨过该行来确定它是该行,并且跨过该特定行是引发异常的原因。

编辑:

我能够制作一个 100% 重现此问题的基本项目,这让我相信这是 iOS 错误,而不是我的代码。

  1. 创建一个新项目。选择单视图应用程序。不管是故事板还是基于 xib 的
  2. 打开 iPad xib/storyboard,在视图中添加一个圆形矩形按钮
  3. 将以下 IBAction 添加到视图控制器。 pickerPopoverController__strong ivar

    -(void)iMakeItCrash:(UIButton*)sender
    {
        UIImagePickerController* ip = [[UIImagePickerController alloc] init];
        ip.delegate = self;
        ip.allowsEditing = YES;
        ip.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        ip.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) (kUTTypeImage), nil];
    
        pickerPopoverController = [[UIPopoverController alloc] initWithContentViewController:ip];
        [pickerPopoverController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
    
  4. 将此 IBAction 连接到按钮的 Touch Up Inside 事件。

  5. 在模拟器上运行,在 iPad 上崩溃

编辑2:

如果我尝试使用presentPopoverFromBarButtonItem:,仍然会发生崩溃。但是,如果我根本不提供图像选择器,也不会崩溃...

【问题讨论】:

  • 如果 imagePicker 是一个属性试试 self.imagePicker = [[UIImagePickerController alloc] init];
  • 它不是一个属性,只是一个 ivar
  • 我想你只是评论 imagePicker.allowsEditing = YES;
  • @Spynet:这并不能解决问题,我也不知道如何在崩溃的行之后设置属性会导致问题
  • 在初始化之前检查可用的媒体类型(使用availableMediaTypesForSourceType:isSourceTypeAvailable:)?

标签: ios ipad uiimagepickercontroller


【解决方案1】:

当我的应用在我的任何设备或模拟器上的一个空的“已保存的照片”相册中崩溃时,我注意到了这一点。如果“已保存的照片”中有照片,则该错误不会发生。如果您在模拟器上使用“重置数据和设置”并将相册留空,则很容易复制。

我花了很长时间试图找到一种解决方法,但我一直没能找到。我认为提交 iOS 错误报告是一个非常好的主意。

【讨论】:

    【解决方案2】:

    我遇到过同样的问题,用下面的代码解决了:

    -(IBAction)actionOpenPhotoLibrary:(id)sender
    {
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
        return;
    }
    
    if ([popover isPopoverVisible]) {
        [popover dismissPopoverAnimated:YES];
        return;
    }
    
    UIImagePickerController *imagePicker = [[[UIImagePickerController alloc] init]autorelease];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePicker.delegate = self;
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
    
        popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];              
        popover.delegate = self;
        [popover setPopoverContentSize:CGSizeMake(320, 460)];
        [popover presentPopoverFromBarButtonItem:[[[UIBarButtonItem alloc]initWithCustomView:(UIButton*)sender] autorelease] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
    
    imagePicker.navigationBar.tintColor = APP_THEME_COLOR;
    [self presentModalViewController:imagePicker animated:YES];
    }
    

    祝你好运.....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      相关资源
      最近更新 更多