簡體   English   中英

ios應用程序內存不足

[英]ios app running out of memory

我正在開發一個從照片庫導入照片的應用程序。 導入照片時,使用大量內存,導入160張照片后會崩潰。

我不知道為什么會這樣。 任何幫助將非常感激。

這是我的代碼

- (void)agImagePickerController:(AGImagePickerController *)picker    didFinishPickingMediaWithInfo:(NSArray *)info{

[pickerView dismissViewControllerAnimated:YES completion:nil];
[self.navigationItem setHidesBackButton:YES];
[self.navigationItem.rightBarButtonItem setEnabled:NO];

MBProgressHUD * hud =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText=@"Importing Your Photo(s)";



// USING GRAND CENTRAL DISPATCH

firstQueue=dispatch_queue_create("com.import.photos", nil);
dispatch_async(firstQueue, ^{

    [self beingBackgroundUpdateTask];

    // Open connection
    //[albumObject openDB];


    // Start the loop from here ******************************************************
    int j=0;
    for(j=0;j<info.count;j++){

        // update the hud label
        hud.labelText=[[NSString alloc] initWithFormat:@"Importing %i of %i",j+1, [info  count]];


        // Create new image from returned array
        ALAssetRepresentation *rep = [[info objectAtIndex: j] defaultRepresentation];
        UIImage * image  = [UIImage imageWithCGImage:[rep fullScreenImage]];
        //rep=nil;

        // resize the image for the thumbnail and save it in new image
        UIImage *newImage=[image  resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:CGSizeMake(75, 75)  interpolationQuality:kCGInterpolationDefault];


        NSDate *date = [NSDate date];

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
        [dateFormat setDateFormat:@"DD_MM_YY_HH_mm_ss"];
        NSString *dateString = [dateFormat stringFromDate:date];

        int random=rand();
        // create new file names
        NSString *fileName=[[NSString alloc] initWithFormat:@"%d%@",random,dateString];
        NSString *s_fileName=[[NSString alloc]  initWithFormat:@"small_%d%@",random,dateString];
        // where to save this image
        NSString *imagePath=[self.documentsDirectory  stringByAppendingPathComponent:fileName];
        NSString *s_imagePath=[self.documentsDirectory stringByAppendingPathComponent:s_fileName];


        NSLog(@"type: %@",[rep.url pathExtension]);

        if ([[rep.url pathExtension] isEqualToString:@"JPG"]) {
            // its jpg
            NSData *rawImage=UIImageJPEGRepresentation(image, 1.0);
            NSData *thumb=UIImageJPEGRepresentation(newImage, 1.0);


            image=nil;
            newImage=nil;
            date=nil;
            dateFormat=nil;
            dateString=nil;
            random=nil;

            // Create the images
            [rawImage writeToFile:imagePath atomically:YES];
            [thumb writeToFile:s_imagePath atomically:YES];

            imagePath=nil;
            s_imagePath=nil;
            rawImage=nil;
            thumb=nil;

        }

        if ([[rep.url pathExtension] isEqualToString:@"PNG"]) {
            // its PNG
            // save it in NSData to it can be saved in the directory
            NSData *rawImage=UIImagePNGRepresentation(image);
            NSData *thumb=UIImagePNGRepresentation(newImage);




            image=nil;
            newImage=nil;
            date=nil;
            dateFormat=nil;
            dateString=nil;
            random=nil;

            // Create the images
            [rawImage writeToFile:imagePath atomically:YES];
            [thumb writeToFile:s_imagePath atomically:YES];

            imagePath=nil;
            s_imagePath=nil;
            rawImage=nil;
            thumb=nil;

        }





        // Now save it to database


        [albumObject savePhoto:fileName toFID:PassedID toName:s_fileName  albumName:singletonObj.currentAlbum photosName:singletonObj.currentPhotos];


        [display_photos addObject:[MWPhoto photoWithFilePath:[self.documentsDirectory  stringByAppendingPathComponent:fileName]]];

        UIImage *img=[UIImage imageWithContentsOfFile:[self.documentsDirectory  stringByAppendingPathComponent:s_fileName]];

        [collection_photos addObject:img];

        img=nil;

        NSArray *pObj=[[NSArray alloc] initWithObjects:fileName,s_fileName, nil];
        [photos addObject:pObj];

        pObj=nil;
        fileName=nil;

        s_fileName=nil;


    } j=nil;

    // Update the album photo count to reflect the changes
    [albumObject updateAlbumCount:PassedID photocount:[photos count]  albumName:singletonObj.currentAlbum];
    photos=[albumObject getPhotos:PassedID photosName:singletonObj.currentPhotos];

    // End the loop here ***************************************************************

    dispatch_async(dispatch_get_main_queue(), ^{
    [self.collectionView reloadData];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [self.navigationItem setHidesBackButton:NO];
    [self.navigationItem.rightBarButtonItem setEnabled:YES];

    });

    });



}

在循環中,生成了許多額外的對象,並且在整個循環完成之前它們不會被釋放。 但它永遠不會完成,因為額外的對象只是在內存耗盡之前不斷堆積。 第一道防線是將循環內部包裝在@autoreleasepool塊中。

例如,請查看我的書的這一部分: http//www.apeth.com/iOSBook/ch12.html#_autorelease向下滾動到該部分的底部。

for /* loop iterator goes here */ {
    @autoreleasepool {
        // everything inside the for loop goes here
    }
}

編輯:我有另一個建議:你最終得到一系列的圖像或類似的東西? 我不能說。 如果是這樣,那么不要將圖像存儲在內存中作為所有這些的最終結果,而是將圖像保存在磁盤上並存儲其URL或文件路徑。 這樣您只需在需要時獲取圖像。 圖像在內存中占用了大量空間,並且隨着大小呈指數增長。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM