简体   繁体   English

内存使用随着图像尺寸的增大而不断增加

[英]Memory uses continuously increasing with large size of images

I have two large size images in the my view controller. 我的视图控制器中有两个大尺寸图像。 The size of images are approximately 1500 x 1800. When I load that view controller a few times I am getting a memory warning and finally my app crashes. 图片的大小约为1500 x1800。当我几次加载该视图控制器时,我收到了内存警告,最后我的应用程序崩溃了。 I did observe some memory uses. 我确实观察到一些内存使用情况。 When I load the view controller my memory use is increased by 15 Mbs each time. 加载视图控制器时,每次我的内存使用量增加15 Mb。 I have read about image caching, so to avoid image caching I am not loading image from controller, instead I am loading image in the following way : 我已经阅读了有关图像缓存的内容,因此为了避免图像缓存,我没有从控制器加载图像,而是通过以下方式加载图像:

  UIImage * image = [[UIImage alloc] initWithContentsOfFile:pathForImageFile1];
  imageView.image = image;
  image = nil;

I have declared image view inside .h file like : 我已经在.h文件中声明了图像视图,例如:

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

By the documentation, If I use load image with initWithContentsOfFile method then image should not be cached and memory should be released once unloading of view happens. 根据文档,如果我将加载图像与initWithContentsOfFile方法一起使用,则应在视图卸载后不缓存图像,并释放内存。

Further I tried to nil image : 进一步,我试图使图像为零:

-(void)viewDidDisappear:(BOOL)animated
{
  imageView = nil;
}

But there is no difference in memory usage. 但是内存使用率没有差异。 How can I release the memory used by imageview? 如何释放imageview使用的内存?

Setting imageView IBOutlet to nil will not unload your imageView because strong reference still remains in imageView 's superview ( subviews array). imageView IBOutlet设置为nil不会卸载您的imageView因为strong引用仍然保留在imageViewsuperviewsubviews数组)中。

Unload image using: 使用以下方法卸载图像:

-(void) viewDidDisappear:(BOOL)animated {
  [super viewDidDisappear:animated];
  imageView.image = nil;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM