本文介绍了iPhone中的内存问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用单个UIImageView对象,为它分配了一次内存.但是要多次更改图像名称.问题是,

I am using single UIImageView object, allocated memory it once. But changing the image names multiple times.The question is that,

A)一个UIImageView对象,仅分配一次,并且从未更改过图像名称.例如,UIImageView * firstObj = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue.png"]];

A) one UIImageView object,allocated once and never changed image name. e.g UIImageView *firstObj = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"blue.png"]];

B)另一个UIImageView对象,该对象分配一次并多次更改图像名称

B) another UIImageView object,allocated once and changed image names multiple times

例如,UIImageView * secondObj = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue.png"]];

e.g UIImageView *secondObj = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"blue.png"]];

//一次更改图像名称,

//changed image name once,

secondObj.image = [UIImage imageNamed:@"green.png"];

secondObj.image = [UIImage imageNamed:@"green.png"];

 and so on....

哪些对象使用最大内存或使用相等的内存?

which objects uses maximum memory or use equal memory?

哪种是在内存利用率最低的情况下使用secondObj的最佳方法?

which is the best way to use secondObj with minimum memory utilization?

请简要解释一下,因为我需要在项目中使用图片数量,并且我想避免由于图片而导致的内存问题.

please explain it briefly because i need to use number of image in my project and i want to avoid memory issue due to the images.

推荐答案

重新格式化代码,第二个示例是:

Reformatting your code, your second example is:

UIImageView *secondObj = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue.png"]];
// changed image name once
secondObj.image = [UIImage imageNamed:@"green.png"];
// and so on....

此代码很好.当您将图像分配给UIImageView的实例时,它将使图像的保留计数增加一.如果图像已经被辅助,它将首先释放它.

This code is fine. When you assign an image to an instance of UIImageView, it will increase the image's retain count by one. If an image is already assiged, it will release it first.

由于 [UIImage imageNamed:...] 将为您提供一个已标记为自动释放的对象,因此您可以继续分配示例中显示的图像,而不会发生内存泄漏.UIImageView释放现有图像后,它将由自动释放池收集.

Since [UIImage imageNamed:...] will give you an object already marked for autorelease, you can continue to assign images as shown in the example with no memory leaks. Once the UIImageView releases the existing image, it will be collected by an Autorelease pool.

就最大程度地减少内存使用而言, [UIImage imageNamed:...] 方法确实将图像存储在少量应用程序范围的高速缓存中,您无需施加任何直接控制权超过.缓存确实有上限,但是您无法刷新它来回收内存,因此使用它会在获取新的UIImage时增加内存占用.

In terms of minimising memory usage, the [UIImage imageNamed:...] method does store the image in a small amount of application-wide cache memory that you don't exert any direct control over. The cache does have an upper limit limit, but you can't flush it to reclaim memory, so using it will increase your memory footprint as you fetch new UIImages.

您可能要考虑通过使用 [UIImage imageWithData:...] 加载图像来避免此缓存,这在Stackoverflow问题 [UIImage imageNamed…]与[UIImage imageWithData…] .

You may want to consider avoiding this cache by using [UIImage imageWithData:...] to load your images, which is discussed in the Stackoverflow question [UIImage imageNamed…] vs [UIImage imageWithData…].

这篇关于iPhone中的内存问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:00