我有一些实例,在我的应用程序中使用 UIImagePickerController 和 UIWebView,将它们呈现为应用程序根 Controller 的模态视图 Controller 。不过,在很大程度上,这些会导致内存警告,从而导致我的“正常” View (应用程序的当前 UI)被释放,并随后在模态视图完成时重新分配。结果是,例如,用户将选择“图像选择器”并用相机拍照,但然后按“确定”并返回到已返回到类似于启动状态的应用程序(因此忘记了)如何处理返回的图像)。它不会 100% 发生(甚至 50%),但足以成为一个大问题。
我的问题是:有什么方法可以防止这种情况发生,本质上是在显示这些模态视图时暂时减少内存占用,从而避免出现此问题?
这非常令人沮丧,因为一旦 UIImagePickerController 模式启动,我对应用程序的控制就很少了...并且返回图像时会发生内存警告,所以我没有机会处理问题。到目前为止,我已经尝试在模式启动时从 Root View 中删除所有 View 等,但运气不佳:(
最佳答案
你的问题对每个人来说都很明显,甚至我也面临这个问题。实际问题出在 iPhone ImagePickerController 相机中... Apple 可能知道这个问题并且可能正在尝试解决这个问题... 除此之外我们能做的就是遵循内存管理规则。
http://www.slideshare.net/whalec1/ios-memory-management-basis
是一个值得遵循的很好的教程。
我使用下面的代码来管理我的应用程序中的内存...它在某种程度上对我有用,但无法完全解决它。
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *diskCachePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"];
if ([manager fileExistsAtPath:diskCachePath])
{
NSError *error=nil;
NSArray *files = [manager contentsOfDirectoryAtPath:diskCachePath
error:&error];
if(!error)
{
for(NSString *file in files) {
[manager removeItemAtPath:[diskCachePath stringByAppendingPathComponent:file]
error:nil];
}
}
}
int i=0;
for(id controller in self.tabBarController.viewControllers)
{
if(i==presentTabIndex)
{
i++;
continue;
}
if([controller isKindOfClass:[UINavigationController class]])
{
UINavigationController *aController = (UINavigationController *)controller;
[aController popToRootViewControllerAnimated:YES];
}
else if([controller isKindOfClass:[UIViewController class]])
{
UIViewController *aController = (UIViewController *)controller;
[aController.navigationController popToRootViewControllerAnimated:YES];
}
i++;
}
}
关于iphone - iOS UIImagePickerController 和 UIWebView 导致内存问题;呈现这些 View 时如何保留应用程序的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8689009/