我使用以下代码在imageView中加载了100多张图片:
[imgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",prefixName,counter]]];其中上面的代码将被调用每个0.03s,counter是用于加载不同图像的数字。
上面的想法给了我动画效果,它用来在0.03s上加载不同的图像。
但当图像更多时,它会用到crash。
我该怎么做?或者,在我的情况下,我如何维护内存?有什么想法吗?
发布于 2011-12-23 19:52:13
试着使用
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];发布于 2011-12-23 19:58:27
您可能正在执行以下操作:
[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];https://stackoverflow.com/questions/8615687
复制相似问题