2013-02-17 65 views
0

我想從iphone相機卷得到最後一張照片。我使用下面的代碼:用AlAssetsLibrary讀取iphone相機卷時的內存問題

UIImage* __block image = [[UIImage alloc] init]; 
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; 

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup* group, BOOL* stop) { 

    [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 

    if ([group numberOfAssets] > 0) { 
     [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1] options:NSEnumerationConcurrent usingBlock:^(ALAsset* alAsset, NSUInteger index, BOOL* innerStop) { 
      if (alAsset) { 
       ALAssetRepresentation* rawImage = [alAsset defaultRepresentation]; 
       image = [UIImage imageWithCGImage:[rawImage fullScreenImage]]; 
       [self doTheJobWithImage:image]; 
       // Inside doTheJobWithImage: a segue is also performed at the end 
      } 
     }]; 
    } 
} failureBlock:^(NSError* error) { 
    NSLog(@"Error: %@", [error localizedDescription]); 
}]; 

它的工作原理但缺點(我使用的儀器和殭屍進行調試):

  • 看來攝像頭讀取的卷內每一個畫面。所以,第一個問題:不enumerateAssetsAtIndexes:只檢索指定的圖像(atIndexes)?我的代碼有什麼問題?
  • 添加到上一個問題,當大量照片處於相機膠捲中時,內存會成爲問題。我的應用程序崩潰,如果太多,或者如果它的工作,似乎檢索圖像永遠不會被釋放,所以第二次或第三次我調用此代碼,它由於內存泄漏無論如何崩潰。第二個問題:如何強制我的代碼在調用doTheJobWithImage後釋放所有內容:?

我使用的是xcode 4.6,ios 6和ARC。提前致謝。

回答

1

幾小時後,我發現這裏的代碼沒有問題。它是提取相機膠捲中最後一張照片的正確代碼(如果有人需要它)。問題是內部doTheJobWithImage:我解決它取代

[self doTheJobWithImage:image]; 

和存儲所產生的圖像別的地方,然後執行doTheJobWithImage:圖片枚舉已完成以後。說這句話的,以防萬一別人有興趣。