我有一个 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];
这是崩溃消息:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSOrderedSet initWithOrderedSet:range:copyItems:]: range {8, 1} extends beyond bounds [0 .. 0]'
我通过尝试在 Debug模式下跨过该行来确定这正是该行,并且跨过该特定行是导致抛出异常的原因。
编辑:
我能够制作一个 100% 重现此问题的基本项目,这让我相信这是一个 iOS 错误,而不是我的代码的问题。
- 创建一个新项目。选择单一 View 应用程序。无论是 Storyboard还是基于 xib 都没有关系
- 打开 iPad xib/storyboard,向 View 添加一个圆形按钮
将以下 IBAction 添加到 View Controller 。
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]; }
将此 IBAction 关联到按钮的 Touch Up Inside 事件。
- 在模拟器上运行,在 iPad 上崩溃
编辑2:
如果我尝试使用 presentPopoverFromBarButtonItem:
,仍然会发生崩溃。但是,如果我根本不显示图像选择器,则不会崩溃...
最佳答案
当我的应用程序在我的任何设备或模拟器上的空“已保存照片”相册中崩溃时,我注意到了这一点。如果“保存的照片”中有照片,则不会发生该错误。如果您在模拟器上使用“重置数据和设置”并将相册留空,则很容易复制。
我花了很长时间试图找到解决方法,但一直没能找到。我认为提交 iOS 错误报告是一个真正的好主意。
关于ios - UIImagePickerController 在 alloc/init 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10702979/