3

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

    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伊瓦尔

    -(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:. 但是,如果我根本不提供图像选择器,也不会崩溃......

4

2 回答 2

2

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

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

于 2012-05-25T21:44:49.670 回答
0

我曾经遇到过同样的问题并使用以下代码修复:

-(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];
}

祝你好运......

于 2012-05-25T03:56:58.267 回答