我需要查找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它顯示我錯誤的計數所有圖像,請讓我知道我做錯了什麼。
NSData的長度 返回包含在接收的字節數。 - (NSUInteger)length https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html – Rajneesh071