我最近遇到内存崩溃,我怀疑我没有清空数组,这是我的数组代码
- (void)viewDidLoad
{
allImagesArray = [[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *location=@"Bottoms";
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
collectionBottoms.delegate =self;
collectionBottoms.dataSource=self;
for(NSString *str in directoryContent){
NSLog(@"i");
NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[allImagesArray addObject:image];
NSLog(@"array:%@",[allImagesArray description]);
}}
}
如何通过释放数组中的对象来释放内存?
最佳答案
您将图像数组保存在 Plist 中。保存后你只需重新初始化你的数组,它将清除数组中的内存。那你用不ARC?因为 ARC 更适合这个。然后在 -viewWillDisappear 中重新初始化或从数组中删除 AllObject
关于ios - 数组只是不断添加对象和内存崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19580903/