我需要在 iPhone 的照片库中找到所有图像的总大小。我使用了资产库框架并成功找到了图像数量。但是图像尺寸较小,将它们转换为 MB 会给我错误的结果。下面是源代码。
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 时,它向我显示了所有图像的错误计数,请让我知道我做错了什么。