2015-06-09 49 views
0

我在文檔目錄中存儲了沒有圖像(例如:考慮60+)。我必須在桌面視圖中顯示所有這些圖像。這些圖像大,所以我調整圖像按照該鏈接http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/如何在沒有記憶警告的情況下正確調整圖像大小

這裏是我做的代碼在cellforrow是:

@autoreleasepool { 

      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); 
      dispatch_async(queue, ^{ 


        NSString *folderPath = [self getEduCurrentDiractoryPath]; 
        NSString *filePath = [folderPath stringByAppendingPathComponent:[message.internalUrl.absoluteString lastPathComponent]]; 
        NSLog(@"currentDirectoryPath %@",filePath); 

        dispatch_sync(dispatch_get_main_queue(), ^{ 

         cell.message.thumbnail = [[UIImage imageWithContentsOfFile:filePath] resizedImage:CGSizeMake(210, 170) interpolationQuality:kCGInterpolationLow]; 

        }); 
      }); 
     } 

現在,當我用儀器檢查,看看分配和試圖找到內存警告的原因它顯示如下結果:

所以在這裏你可以看到resizeimage:interpolationQuality方法顯示出168MB的分配,我的項目ARC是,我已經把這些代碼在@autoreleasepool太..但它不釋放分配內存...

enter image description here

任何一個可以幫助我如何正確rezise圖像wiout越來越內存警告或某些可以指出我在哪裏犯錯..任何幫助將不勝感激.. !!!謝謝..

+0

沒有意義使用異步塊來讀取圖像的路徑。它的操作佔用一般執行時間的000001%。裁剪操作中代碼中最繁瑣的操作和最耗時的操作。所以,我建議你把: UIImage * croppedImage = [[UIImage imageWithContentsOfFile:filePath] resizedImage:CGSizeMake(210,170)interpolationQuality:kCGInterpolationLow]; 在異步塊中獲取圖像: cell.message.thumbnail = croppedImage; 在主線程中。 並嘗試將@autoreleasepool替換爲異步塊。 – David

+0

並通過Instrument Allocations檢查性能。如果確實如此,那麼嘗試刪除它,並檢查儀器的差異。 – David

+0

謝謝..我也試過這個選項了......建議的代碼..情況變得更糟...... – Wolverine

回答

0

試試這個。這裏imgProf是我的圖像名稱。

imgProf = [self imageWithImage:imgProf scaledToSize:CGSizeMake(400, 400)]; 

這是我創建的函數來調整圖像大小。

- (UIImage*)imageWithImage:(UIImage*)image 
       scaledToSize:(CGSize)newSize; 
{ 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 
0

不要在cellforrow方法中編碼它,你應該在reloadData之前完成你的縮略圖。

相關問題