ios - 数组只是不断添加对象和内存崩溃

标签 ios arrays memory

我最近遇到内存崩溃,我怀疑我没有清空数组,这是我的数组代码

- (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/

相关文章:

assembly - E820h QuerySystemAddressMap 在 4GB+ 内存上返回的值

java - "variable name"占用多少空间?

swift - SpriteKit 在我离开游戏后保留内存

ios - 如何更改 Kal 日历中的单元格高度

ios - 使大型 UIView 适合内容 View

javascript - JavaScript 数组中的最小和最大索引

c - 为什么数组保存 C 中第一个元素的地址?

ios - 如何使用授权码流程为 IOS 客户端设置 Identity Server 3

ios - 模态关闭后显示错误消息

c++ - 如何创建接受二维数组的构造函数