iPhone:如何在ImageView中管理130个图像?

iPhone:如何在ImageView中管理130个图像?,iphone,objective-c,ios,cocoa-touch,ios4,Iphone,Objective C,Ios,Cocoa Touch,Ios4,我有100多个图像,我使用以下代码在imageView中加载这些图像: [imgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",prefixName,counter]]]; 其中,上述代码将每0.03s调用一次,计数器是用于加载不同图像的编号 上面的想法给了我动画效果,它用0.03秒加载不同的图像 但是当图像更多时,它通常会崩溃 我该怎么办?或者在我的案例中我如何保持记忆?有什么想法吗?试着用 N

我有100多个图像,我使用以下代码在imageView中加载这些图像:

   [imgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",prefixName,counter]]];
其中,上述代码将每0.03s调用一次,
计数器
是用于加载不同图像的编号

上面的想法给了我动画效果,它用0.03秒加载不同的图像

但是当图像更多时,它通常会崩溃

我该怎么办?或者在我的案例中我如何保持记忆?有什么想法吗?

试着用

NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
[UIImage imageWithData:imageData];
而不是

[imgV setImage:[UIImage imageNamed:fileName.extension]];
此外,UIImageView还具有setAnimationImages方法

imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[imgView setAnimationImages:myArray];//myArray - array of Images
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount=0];
[imgView startAnimating];
[self addSubview:imgView];
尝试使用

NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
[UIImage imageWithData:imageData];
而不是

[imgV setImage:[UIImage imageNamed:fileName.extension]];
此外,UIImageView还具有setAnimationImages方法

imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[imgView setAnimationImages:myArray];//myArray - array of Images
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount=0];
[imgView startAnimating];
[self addSubview:imgView];

您可能正在这样做:

[imgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",prefixName,counter]]];
在一个循环中。每个
imageNamed:
对象都已分配,但只有在自动释放池耗尽时才会释放。请尝试改用此代码:

UIImage *img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@%d",prefixName,counter] ofType:@"png"]];
imgV.image = img;
[img release];

您可能正在这样做:

[imgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",prefixName,counter]]];
在一个循环中。每个
imageNamed:
对象都已分配,但只有在自动释放池耗尽时才会释放。请尝试改用此代码:

UIImage *img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@%d",prefixName,counter] ofType:@"png"]];
imgV.image = img;
[img release];

+对于setAnimationImages为1,但在内存中存储100个图像可能会导致内存占用过大和崩溃,因此一个很可能不正确。据我们所知,每个图像的大小可能只有10kb,并且没有内存问题。+1对于setAnimationImages,但在内存中存储100个图像可能会导致内存占用过大和崩溃,因此,这一点很可能是不正确的。据我们所知,每个图像的大小可能只有10kb,并且没有内存问题。请尝试预创建图像,而不是像您所做的那样在一行中加载和设置它们。通过设置一个调度队列来加载和缓存图像,可以显示与声音同步的多分钟视频。请复制并粘贴崩溃日志尝试预先创建图像,而不是像您所做的那样在一行中加载和设置它们。通过设置调度队列来加载和缓存图像,可以显示与声音同步的多分钟视频。请复制并粘贴崩溃日志