0

画面の上部にある進行状況メーターを使用して、600 を超える画像をループでダウンロードしようとしています。アクティビティと進行状況を表示するために、画面をフェード レイヤーでブロックしました。

間にメモリ警告メッセージが表示され、アプリがクラッシュします。

ループに到達するための私の手順は次のとおりです。

  1. アプリ デリゲートで、isImageAvailable bool フィールドの値が「0」であるすべての行の最初のコア データ テーブルをチェックします。
  2. いくつかのカウント (たとえば 600) が表示されたら、YES と NO のオプションを表示してアラートを出します。
  3. YES の場合: [self performSelector:@selector(myDownload:) withObject:nil afterDelay:0.2];
  4. マイダウンロードで

    NSOperationQueue *queue = [NSOperationQueue new];
    // Create our NSInvocationOperation to call loadDataWithOperation, passing in nil 
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                     selector:@selector(startUpdatingRecords:) object:nil];
    
    // Add the operation to the queue 
    [queue addOperation:operation];
    [operation release];
    [queue release];
    
  5. startUpdatingRecords で:

    -(void)startUpdatingRecords:(id)sender 
    {    
    [self performSelectorInBackground:@selector(updateProgressMeter:) withObject:    [NSString stringWithFormat:@"%d",self.loopStartIndex]];
    
    // Variable declarations             
    CGSize newSizeLarge ;
    NSPredicate *predicate;
    NSMutableArray *MatchingID;
    Image_DB *data;
    
    // Cache Directory path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,  YES);
    
    NSData *responseData; // = [[NSData alloc]init] ;
    
    NSURL *url = [[[NSURL alloc]init] autorelease];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]init] autorelease];
    UIImage *imgSelected_Large = [[[UIImage alloc]init] autorelease];
    
    // Loop through all IDs
    for (int i = 0; i < [self.arrayOfID count]; i++) //for (int i = loopStart; i < loopEnd; i++) 
    { 
    if (self.abortDownload)
    {           
        break;
    } 
    
    NSString *documentsDirectory = [[[NSString alloc] initWithFormat:@"%@",[paths objectAtIndex:0]] autorelease];
    documentsDirectory = [paths objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingFormat:@"/ImageFolder"]; // Image folder path
    
    myClass *classObj = [self.arrayOfID objectAtIndex:i];   
    
    NSString *strURl = [[[NSString alloc] initWithFormat:@"%@%@", self.MyURL,recipeObj.recipeImageStr] autorelease];
    //NSLog(@"URL = %@",strURl);           
    
    url = [NSURL URLWithString:strURl];
    request = [NSMutableURLRequest requestWithURL:url];
    responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL]; // Get Image Data into NSData
    
    //imgSelected_Large = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strURl]]];
    
    NSLog(@"Download Count = %d",i+1);
    
    if (responseData != nil)
    {
        imgSelected_Large = [UIImage imageWithData:responseData];
    
        // Resizining image
        newSizeLarge.width = 320;    
        newSizeLarge.height = 180;     
    
        imgSelected_Large = [self imageWithImage:imgSelected_Large scaledToSize:newSizeLarge]; // New sized image
        NSData *dataPhoto; // no need to release it because UIImageJPEGRepresentation gives autoreleased NSData obj.
        dataPhoto  = UIImageJPEGRepresentation(imgSelected_Large, 0.6);  // Set new image representation and its Compression Quality
    
        documentsDirectory = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Image_%d", classObj.nodeID]];            
        [dataPhoto writeToFile:documentsDirectory atomically:YES]; //Write file to local folder at default path
    
        predicate = [NSPredicate predicateWithFormat: @"(image_ID = %d )",recipeObj.nodeID];
        MatchingID = [CoreDataAPIMethods searchObjectsInContext:@"Image_DB" :predicate :@"image_ID" :YES :self.managedObjectContext];
    
        // Setting flag variable for available image
        for (int j = 0; j< [MatchingID count]; j++)
        {
            //Assign the Authors Records in Class Object and save to Database 
            data = (Image_DB*) [MatchingID objectAtIndex:j];                
            // data.image_large = dataPhoto; // Code for storing BLOB object to DB
            data.extra_1 = @"1";
            //NSLog(@"Flag updated");
        }
    }
    // Exit out code
    if ( i == [self.arrayOfID count] - 1 || i == [self.arrayOfID count]) // Its the last record to be stored
    {    
        NSError *error;             
        if (![self.managedObjectContext save:&error])
        {
            // Handle the error...
            NSLog(@"Error in updating %@",error);
        } 
    
        self.isUpdateImageCalled = NO;
        [self performSelectorOnMainThread:@selector(removeProgressMeter) withObject:nil waitUntilDone:NO];
    
    }
    // Update UI screen while in downloading process
    [self performSelectorInBackground:@selector(updateProgressMeter:) withObject:[NSString stringWithFormat:@"%d",self.loopStartIndex+i+1]];
    }
    }
    

responseData を解放しなかった場合、アプリにメモリ警告が表示され、クラッシュしました。それで解放したら、 [NSConcreteMutableData release]: message sent to deallocated instance 0x1e931de0 エラーが発生します。

コードを改良する方法。誰でも私のコードを提案して、作り直して洗練されたコードを作成できますか。

助けてください。

4

1 に答える 1

2

responseDataによって返されたものは自動リリースされるため、自分sendSynchronousRequestでリリースしないでください。一見したところ、コードにメモリリークは見られません。アプリケーションが実際にメモリをリークせずに使用しすぎている可能性があります。forサイクル内に自動解放プールを配置してみてください。

for (...) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // your original code with a lot of autoreleased objects

    [pool release];
}

自動解放プール内でコードをラップする場合、ラップ内でメッセージが送信されるすべてのオブジェクトはautorelease、プール自体が解放されるときに実際に解放されます。このようにして、サイクルごとにメモリをパージします。

ドキュメントの「自動解放プールの使用」も参照してください。「多くの一時オブジェクトを作成するループを作成する場合」の場合に使用する必要があることが具体的に示されています。

于 2012-04-06T07:48:59.687 に答える