【问题标题】:Leaking detected when presenting UIImagePickerController呈现 UIImagePickerController 时检测到泄漏
【发布时间】:2017-06-16 20:36:44
【问题描述】:

这是我用来展示UIImagePickerController的代码:

- (IBAction)takePhoto:(UIButton *)sender {

  if (![UIImagePickerController
          isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIAlertController *alert = [UIAlertController
        alertControllerWithTitle:NSLocalizedString(@"Error", nil)
                         message:NSLocalizedString(@"Device has no camera", nil)
                  preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok =
        [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
                                 style:UIAlertActionStyleDefault
                               handler:nil];
    [alert addAction:ok];
    [self presentViewController:alert animated:YES completion:nil];

  } else {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.videoQuality = UIImagePickerControllerQualityTypeMedium;

    [self presentViewController:picker animated:YES completion:NULL];
  }
}

目前我运行这个方法(例如点击UIButton)我可以在仪器中看到这个:

如果我更改为 Cycles & Roots,这就是我看到的:

因此,如果我将鼠标悬停在第一个泄漏上,然后按下弹出的箭头,我会得到:

如果我打开此方法的堆栈跟踪,我会看到:

所以它主要是系统调用。其他泄漏也是如此,只是系统调用......所以这是一个错误还是?如果我打开并关闭图像选择器几次,我会得到更多的泄漏,更多......

有人注意到了吗?

【问题讨论】:

  • 正如我在bisma's answer 的评论中已经提到的,在回调方法中我只有一个关闭选择器的代码:[picker dismissViewControllerAnimated:YES completion:nil];

标签: ios objective-c xcode memory-leaks xcode-instruments


【解决方案1】:

您需要使 UIImagePickerController *picker 成为一个强大的属性。因此,该应用程序具有对该选择器的全局引用,您将能够将其关闭。

【讨论】:

  • 我已经能够以相应的方法将其关闭(参见UIImagePickerControllerDelegate 协议)。在文档中指出:“选择器不会自行关闭;客户端在这些回调中将其关闭。”我的意思是我可以试试你说的,但我目前正在使用这个控件,因为它应该根据文档使用。
猜你喜欢
  • 2012-07-16
  • 2012-01-22
  • 1970-01-01
  • 2017-11-21
  • 2021-09-01
  • 2018-07-08
  • 2019-09-06
  • 2010-10-29
相关资源
最近更新 更多