1

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

- (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]);

        }}
}

我可以通过释放数组中的对象来释放内存吗?

4

3 回答 3

1

您将图像数组保存在 Plist 中。保存后,您只需重新初始化数组,它将清除数组中的内存。那么您是否使用ARC?因为ARC对此更舒服。然后从 -viewWillDisappear 的数组中重新初始化或删除所有对象

于 2013-10-25T04:27:06.737 回答
0

如果您正在使用此图像数组,allImagesArray则当视图可见时,将此代码放入viewWillAppearviewDidAppear方法中UIViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

   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]);

        }
    }
}

并且,然后在 viewWillDisappear/viewDidDisappear 的方法中释放它UIViewController

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [allImagesArray removeAllObjects];
    //[allImagesArray release]; // (Uncomment) Call this only if you are not using ARC.
}

或者,一个快速、肮脏但安全的修复方法是(不知道你的用法allImagesArray) -

- (void)viewDidLoad
{
    [super viewDidLoad];

     // Remove previously added objects.
     if (allImagesArray != nil && allImagesArray.count > 0) {
            [allImagesArray removeAllObjects];
     }
     // Allocate memory if not done earlier (i.e. first time).
     if (allImagesArray == nil) {
        allImagesArray = [[NSMutableArray alloc] init];
     }

     // Rest of your code as stated in your question
}
于 2013-10-25T04:26:33.060 回答
0

嗨,只要对象工作结束就释放内存。这是给你的代码。

- (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)
    {
        NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
        NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
        if(data)
        {
            UIImage *image = [UIImage imageWithData:data];
            [allImagesArray addObject:image];
            NSLog(@"array:%@",[allImagesArray description]);
        image = nil;

        }
    finalFilePath=nil;
    data=nil;
   }

 paths= nil;
 documentsDirectory= nil;
 location= nil;
 fPath= nil;
 directoryContent = nil;
}
于 2013-10-25T04:43:18.743 回答