自iOS 10推出以来,我们几年前开发的应用程序已经开始崩溃。该应用程序重复呈现UIImagePickerControllers,以允许用户捕获主题的多张照片。这样做了40多次后,应用程序崩溃了。这在我们测试过的所有设备上都是可重现的,但在引入iOS 10之前没有发生。我们在Info.plist文件中有NSCameraUsageDescription和NSPhotoLibraryUsageDescription键。
我创建了一个演示问题的最小示例应用。这是最终导致崩溃的代码:
- (IBAction) cameraPressed:(id) sender {
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.delegate = self;
self.picker.allowsEditing = NO;
[self presentViewController:self.picker animated:YES completion:nil];
}
当用户反复使用连接到此IBAction的相机按钮时,它最终开始变慢(拾取器的呈现开始花费更长时间),并最终崩溃(通常在使用相机50次以后)。没有生成崩溃日志,但是当连接到Xcode时,这样的输出会出现在导致崩溃的控制台中:
2016-11-04 20:30:11.884984 WLPBeta[2747:275474] [MC] Invalidating cache
2016-11-04 20:30:11.890776 WLPBeta[2747:273019] [MC] Reading from public effective user settings.
2016-11-04 20:30:13.017608 WLPBeta[2747:275091] [MC] Invalidating cache
2016-11-04 20:30:13.018312 WLPBeta[2747:273019] [MC] Reading from public effective user settings.
2016-11-04 20:30:19.271720 WLPBeta[2747:276311] [MC] Invalidating cache
2016-11-04 20:30:19.279462 WLPBeta[2747:273019] [MC] Reading from public effective user settings.
2016-11-04 20:32:08.229294 WLPBeta[2747:278515] [MC] Invalidating cache
2016-11-04 20:32:08.273941 WLPBeta[2747:273019] [MC] Reading from public effective user settings.
2016-11-04 20:32:09.335711 WLPBeta[2747:278514] [MC] Invalidating cache
2016-11-04 20:32:09.342161 WLPBeta[2747:273019] [MC] Reading from public effective user settings.
2016-11-04 20:32:14.193376 WLPBeta[2747:278515] [MC] Invalidating cache
2016-11-04 20:32:14.213902 WLPBeta[2747:273019] [MC] Reading from public effective user settings.
2016-11-04 20:32:47.944657 WLPBeta[2747:275091] [MC] Invalidating cache
2016-11-04 20:32:47.972053 WLPBeta[2747:273019] [MC] Reading from public effective user settings.
2016-11-04 20:32:48.550934 WLPBeta[2747:279485] [MC] Invalidating cache
2016-11-04 20:32:48.575065 WLPBeta[2747:273019] [MC] Reading from public effective user settings.
2016-11-04 20:32:50.855308 WLPBeta[2747:279485] [MC] Invalidating cache
2016-11-04 20:32:50.856329 WLPBeta[2747:273019] [MC] Reading from public effective user settings.
2016-11-04 20:32:52.201535 WLPBeta[2747:275091] [GatekeeperXPC] Connection to assetsd was interrupted or assetsd died
我还向Apple发布了一个错误报告,并将其添加到开放式雷达中:http://www.openradar.me/radar?id=4941109843197952
有没有人遇到过这个问题?是否有一个修复程序可以让我们在没有使用Xcode 8重新编译应用程序的情况下移过这个?不幸的是,我们正在使用尚未为Xcode 8和iOS 10 SDK做好准备的第三方库,因此我们仍在使用Xcode 7.3.1构建应用程序。
编辑:这里是一个Github repo的链接,其中包含一个可用于演示此问题的示例应用。
https://github.com/lolay/ImagePickerTest
编辑2:如果我更改代码以便在viewDidLoad中分配和初始化选择器,则在使用相机按钮的次数相同后,它仍然会崩溃。
- (void)viewDidLoad {
[super viewDidLoad];
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.allowsEditing = NO;
}
- (IBAction) cameraPressed:(id) sender {
[self presentViewController:self.picker animated:YES completion:nil];
}
答案 0 :(得分:1)
您的UIImagePickerController定义为STRONG。
请在视图被破坏之前将其更改为弱或确保对象为零。
答案 1 :(得分:0)
试试这段代码。
- (IBAction) cameraPressed:(id) sender {
if (self.picker == nil){
self.picker = [[UIImagePickerController alloc] init];
}
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.delegate = self;
self.picker.allowsEditing = NO;
[self presentViewController:self.picker animated:YES completion:nil];
}
答案 2 :(得分:0)
您是否尝试在cameraPressed()函数
中声明Imagepicker变量- (IBAction) cameraPressed:(id) sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsEditing = NO;
[self presentViewController:self.picker animated:YES completion:nil];
}
答案 3 :(得分:0)
您需要明确驳回UIImagePickerController
,即使它似乎是自动执行此操作。如果你不这样做,它可能会保留视图控制器,从而导致内存问题。
来自UIImagePickerControllerDelegate documentation:
如果将图像选择器的showsCameraControls属性设置为NO和 提供您自己的自定义控件,您可以拍摄多张照片 在解除图像选择器界面之前。但是,如果你设置它 属性为YES,您的代理人必须关闭图像选择器界面 用户拍摄一张照片或取消操作后。
默认情况下,该属性为YES,因此您需要关闭。
使用类似于以下的内容:
- (IBAction) cameraPressed:(id) sender {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing = NO;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}