【发布时间】:2012-10-22 01:55:56
【问题描述】:
故事板中有两个UIViewControllers(带有NavigationController)。
我正在访问相机并在 ViewController1 中拍照。我想切换到 ViewController2 并在UIImageView 中显示捕获的图片。
ViewController2 H 文件
@interface ViewController2 : UIViewController
{
-IBOutlet UIImageView *matchImage;
}
-(IBAction)restart:(id)sender;
@property (retain, nonatomic) UIImageView *matchImage;
@end
ViewController1 M 文件
在我的代码拍照后,我尝试使用以下语法切换视图
ViewController2 *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
[self.navigationController pushViewController:vc animated:YES];
[vc.matchImage setImage:tempImage];
我第一次拍照时效果很好,我在 ViewController2 中看到了图像。
但是我在 ViewController2 上有一个后退按钮,语法如下
ViewController2 M文件
-(IBAction)restart:(id)sender
{
ViewController1 *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
[self.navigationController pushViewController:vc animated:YES];
}
这成功返回到 ViewController1,我可以再次拍照并重复该过程。但是在重复该过程大约 3-4 次后,我收到了 Received Memory Warning 并且应用程序崩溃了。在 Xcode 中运行分析器不会产生任何问题。
如果我注释掉以下行
[vc.matchImage setImage:tempImage];
然后我可以执行无休止的捕获(超过 50 个没有问题),但当然 ViewController2 不显示图像(它是空白的)。所以这让我相信我需要释放 matchImage (特别是因为它是一个具有保留属性的属性)。但这似乎没有帮助。任何帮助将不胜感激。我还在学习内存管理。
更新
这里有更多信息,在视图之间来回切换 3 次迭代后,这是调试器的输出。我在两个 ViewController viewDidUnload 和 didReceiveMemoryWarning 方法中放入了 NSLog 语句。因此,当发生 Received memory 警告时,似乎正在卸载 2 个视图控制器的 3 个“实例”。因此,对于视图控制器之间的每次切换迭代,我似乎都得到了一个新副本。这是你所期望的吗?
2012-10-22 08:23:32.008 TestApp[787:707] 收到内存警告。
2012-10-22 08:23:32.015 TestApp[787:707] viewDidUnload ViewController1: 0xfd52250
2012-10-22 08:23:32.017 TestApp[787:707] didReceiveMemoryWarning ViewController1: 0xfd52250
2012-10-22 08:23:32.023 TestApp[787:707] viewDidUnload ViewController2
2012-10-22 08:23:32.025 TestApp[787:707] didReceiveMemoryWarning ViewController2
2012-10-22 08:23:32.028 TestApp[787:707] viewDidUnload ViewController1: 0x16dc30
2012-10-22 08:23:32.030 TestApp[787:707] didReceiveMemoryWarning ViewController1: 0x16dc30
2012-10-22 08:23:32.033 TestApp[787:707] viewDidUnload ViewController2
2012-10-22 08:23:32.037 TestApp[787:707] didReceiveMemoryWarning ViewController2
2012-10-22 08:23:32.040 TestApp[787:707] viewDidUnload ViewController1: 0x171de0
2012-10-22 08:23:32.042 TestApp[787:707] didReceiveMemoryWarning ViewController1: 0x171de0
2012-10-22 08:23:32.044 TestApp[787:707] didReceiveMemoryWarning ViewController2
2012-10-22 08:23:32.046 TestApp[787:707] didReceiveMemoryWarning ViewController1: 0xfd87580
【问题讨论】:
-
尝试强而不是保留,看看是否有效
-
还尝试创建一个属性为
UIImage并将tempImage分配给该属性,然后在viewWillApper:中输入self.matchImage.image = self._your property name_;可能会正常工作:) -
感谢您的建议,但问题仍然存在。我已将一些调试器输出添加到我的原始帖子中。
标签: ios5 memory-management uiviewcontroller uiimage