我怎样才能在iOS中捕获此异常?

时间:2015-10-27 10:48:32

标签: ios exception crash uiimagepickercontroller

以下是我的代码的一部分:

self.rearPicker = [[UIImagePickerController alloc] init];
self.rearPicker.delegate = self;
self.rearPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.rearPicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
self.rearPicker.showsCameraControls = NO;
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:self.rearPicker animated:YES completion:^{
    // send log to my server.And log's success in sending shows that  everything goes pretty well until this line.
    @try {
    // I think it's this line of code that causes crash on some device. What else can I do to avoid crashes besides @try-@catch.
    [self.rearPicker performSelector:@selector(takePicture) withObject:nil afterDelay:3.f];
    }
    @catch (NSException *exception) {
      [self dismissViewControllerAnimated:YES completion:nil];
    }
    @finally {
         // do nothing for now
    }

   }];
});

//根据我的登录服务器和崩溃日志,在某些设备上我的应用程序可以正常执行

[self.rearPicker performSelector:@selector(takePicture) withObject:nil afterDelay:3.f];

//但是在此之后崩溃了。下面有两种崩溃日志:

  1. *** setObjectForKey:object不能为nil(key:UIImagePickerControllerOriginalImage)

  2. ***相机:找到mogul的捕获设备格式时出错

  3. //我真的很困惑。我怎样才能抓住这两个例外?谢谢你的耐心和善意!

1 个答案:

答案 0 :(得分:0)

在@try中,你正在做的就是稍后发送一些东西。 @try不包含稍后发生的事情,只包含执行此操作的请求。

相反,您应该调度到您自己的方法,然后在该方法内部直接在问题调用(takePicture)周围添加异常处理。请注意,如果该调用执行任何异步操作,那么您将遇到类似的问题。

一般情况下,最好添加断点并找出问题所在,以便您可以添加检查以避免这种情况,而不是在事件发生后尝试处理它。