iPhone中高效内存的图像缓存方式

iPhone中高效内存的图像缓存方式,iphone,memory-management,uiimageview,uiimage,image-caching,Iphone,Memory Management,Uiimageview,Uiimage,Image Caching,我想缓存91个图像的图像。所有的图像都是50mb左右。我发现很多内存都用光了 我的密码在这里。高效存储的最佳方式是什么 lG2Image[0] = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"png"]]; lG2Image[1] = [[UIImage alloc] initWithContentsOfFile:[[NSBundle m

我想缓存91个图像的图像。所有的图像都是50mb左右。我发现很多内存都用光了

我的密码在这里。高效存储的最佳方式是什么

lG2Image[0] = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"png"]];
lG2Image[1] = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myimage1" ofType:@"png"]];
lG2Image[2] = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myimage2" ofType:@"png"]];
...


UIImageView* tmp;

for (int i=0; i<91; i++) {
    tmp = [[UIImageView alloc] initWithImage:lG2Image[i]];
    [_window addSubview:tmp];
    [tmp removeFromSuperview];
    tmp = nil;
}
lG2Image[0]=[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“background”of type:@“png”];
lG2Image[1]=[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“png”类型的“myimage1”;
lG2Image[2]=[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“png”类型的“myimage2”;
...
UIImageView*tmp;

对于(int i=0;i您可以重用
UIImageView

iPhone的屏幕大小是固定的,这意味着可视区域是固定的。 例如,如果您想在可视屏幕上显示3个图像,您可以只创建3个
UIImageView
s,当视图移动时,您可以根据当前可视视图的位置加载新图像,不在可视屏幕上的图像可以释放


如果您使用的是
UITableView
,您可以看到这一点。

您有两个选项。选项1,只需“根据需要”加载游戏图像元素,如果这是2D游戏,则在元素即将出现在屏幕上时加载。选项2,将图像数据解压缩为BGRA格式的原始像素文件,然后使用[NSData data WITH CONTENTS OFMAPPEDFILE]和CGDataProviderCreateWithData()以及CGImageCreate()一起创建CGImageRef,然后将其包装为UIImage。第二种方法级别非常低,但它避免了每次访问资源时都必须再次解析图像内容(解压缩的图像数据已存储在文件中)此外,苹果优化了CoreGraphics读取2D图像的方式,这样整个内存内容就可以保留在主内存中,而不是复制到图形内存中,这意味着您可以使用这种方法加载一组图像。如果可以,我会先尝试选项#1,您可能不会同时需要内存中的所有内容。但是选项#2可以为您提供约700兆的工作内存,您的iOS应用程序可以从文件映射这些内存,远远超过作为常规内存分配的内存。

为什么您需要将所有91个图像预加载到内存中?您不能只提前加载一两个图像吗?这是游戏级别。所有图像都在一个级别中。“背景、动画、效果、角色、对象…”游戏就是这样的马里奥。