【问题标题】:UIPageViewController with images crash the app带有图像的 UIPageViewController 使应用程序崩溃
【发布时间】:2012-10-30 11:50:23
【问题描述】:

我使用了 UIPageViewController 我想显示 24 页,每页不是文本而是图像 在 contentview 控制器中,我使用此代码

 self.imgView.image=nil;
 self.imgView.image= [UIImage imageNamed:[NSString  stringWithFormat:@"image%d.jpg",index+1]];

 if(self.categoryId==3 ||self.categoryId==4)
 {
     self.imgView.contentMode=UIViewContentModeScaleToFill;
 }
 else
 {
     self.imgView.contentMode=UIViewContentModeScaleAspectFit;
 }

但问题是当我运行应用程序并翻页时 在 17 或 18 页之后,它给了我一个内存警告,我的应用程序崩溃了

【问题讨论】:

  • 你如何响应内存警告?你真的释放了不可见的内存。一次不需要在内存中保存超过 3 页。那是当前可见的页面和旁边的两个页面。所以 16、17 和 18 当前可见 17。其他所有内容都可能被释放(并在用户滚动/翻页时再次创建)。
  • 我只是在图像视图中设置图像。你能解释一下如何删除其他页面吗?以及如何释放隐形记忆?谢谢

标签: objective-c uipageviewcontroller


【解决方案1】:

您可以尝试使用:

NSString* imgFile = [NSString  stringWithFormat:@"image%d",index+1];
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:imgFile ofType:@"jpg"];
self.imgView.image = [UIImage imageWithContentsOfFile:pathToImageFile];

确实,imageNamed 缓存了您通过它加载的所有图像。缓存越来越大,它会阻止内存被适当地释放。

【讨论】:

  • 谢谢。它的工作原理,但完成导航 3-4 次后应用程序仍然崩溃
猜你喜欢
  • 1970-01-01
  • 2015-09-29
  • 2019-02-13
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
相关资源
最近更新 更多