0

这是我的代码它不会释放内存达到 60 mb 并且应用程序会终止

for (int i=0; i<[modelList count] ;i++) {

    url=@"http://192.168.0.101/images/projectimages/";
    url=[url stringByAppendingString:[modelList objectAtIndex:i]];
    url=[url stringByAppendingString:@".jpg"];


    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];




    NSData *imageData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];

    destinationPath=[documentsDirectory stringByAppendingString:@"/modelimages"];
    destinationPath=[destinationPath stringByAppendingPathComponent:[modelList objectAtIndex:i]];
    destinationPath=[destinationPath stringByAppendingString:@".jpg"];

    [imageData writeToFile:destinationPath atomically:YES];

    [imageData release];
    //imageData=nil;
    //[myurl release];  

    //[imageData release];  

    value=value+divideValue;
    printf("%f\n",value);
    [NSThread detachNewThreadSelector:@selector(updateProgressBar)toTarget:self withObject:nil];

}
4

1 回答 1

2

这很糟糕:

[NSThread detachNewThreadSelector:@selector(updateProgressBar)toTarget:self withObject:nil];

因为它:

  1. 创建了很多线程。
  2. 正如我期望的那样,它会更新 UI,而它应该只从主线程更新。

我认为做这样的事情会更好:

[self performSelectorOnMainThread:@selector(updateProgressBar) 
                       withObject:nil // or any object you need to pass
                    waitUntilDone:NO] //

以及您作为示例给出的方法 - 它应该在单独的线程中运行。在这种情况下,您将有一个后台线程完成所有艰苦的工作,它将通知主线程有关用户界面的更新。

于 2009-09-01T13:46:50.657 回答