2013-07-02 24 views
0

我需要查找iPhone中照片庫中所有圖像的總大小。我使用Assets Library框架併成功查找了多個圖像。然而,圖像尺寸較小,並將它們轉換爲MB,這會給我錯誤的結果。以下是源代碼。如何在iOS中的PhotoLibrary中查找圖像的大小

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 

    [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) 
    { 
     if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { 

      UIImage *image = [UIImage imageWithCGImage:asset.defaultRepresentation.fullResolutionImage]; 
      [self performSelectorOnMainThread:@selector(usePhotolibraryimage:) withObject:image waitUntilDone:NO]; 
     } 
    }]; 
} 
failureBlock:^(NSError *error) { 
    NSLog(@"%@",error.description); 
}]; 


- (void)usePhotolibraryimage:(UIImage *)myImage{ 

    //Do your all UI related and all stuff here 

    NSData *imageData = UIImageJPEGRepresentation(myImage, 0.5); 

    int imageSize = imageData.length/1024; 
    totalImageSize = totalImageSize+imageSize; 
} 

但是,當轉換圖像爲MB它顯示我錯誤的計數所有圖像,請讓我知道我做錯了什麼。

回答

2

使用asset.defaultRepresentation.size

返回用於表示該文件的字節大小。

  • (加長)大小

蘋果DOC:link

+0

NSData的長度 返回包含在接收的字節數。 - (NSUInteger)length https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html – Rajneesh071

0

在你的代碼int imageSize = imageData.length/1024;這將在KB大小顯示。
如果你想在MB中,然後再分爲1024.

並使totalImageSize類型NSInteger

這裏是鏈接爲length of NSData