#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#include<AssetsLibrary/AssetsLibrary.h>
/**
* 获取系统应用所占空间大小
*/
- (void)getSystemApplicationSize
{
__block long long systemApplicationSize = 0;
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSDictionary *dic = [ZZGetApplicationListgetApplicationDiskUsageList];
[dic enumerateKeysAndObjectsUsingBlock:^(id key,id obj, BOOL *stop) {
if (![key hasPrefix:@"com.apple"]) {
systemApplicationSize += [[obj objectForKey:@"Dynamic"]longLongValue];
systemApplicationSize += [[obj objectForKey:@"Static"]longLongValue];
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
UILabel *applicationSizeTemp = (UILabel *)[_systemInformationBjViewviewWithTag:111];
applicationSizeTemp.text = [selfformattedSystemInfo:systemApplicationSize];
});
});
}
/**
* 获取系统音乐所占空间大小
*/
(这个方法也是使得应用变卡的原因,所以后来改成多线程了)
- (NSString *)getSystemMusicSize
{
(这个地方其实不准备 是以每首歌 4M 的大小来计算的,暂时先这样,听康总说360安全卫士也是这么搞的,需要加上边两个头文件)
NSString *systemMusicSize;
int musicUsage = [MPMediaQuerysongsQuery].items.count *4;
systemMusicSize = [self formattedSystemInfo:musicUsage*1024*1024];
return systemMusicSize;
}
(下边这个是改成多线程的后)
/**
* 获取系统音乐所占空间大小
*/
- (void)getSystemMusicSize
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
int musicUsage = [MPMediaQuery songsQuery].items.count * 4;
dispatch_async(dispatch_get_main_queue(), ^{
UILabel *applicationSizeTemp = (UILabel *)[_systemInformationBjView viewWithTag:333];
applicationSizeTemp.text = [self formattedSystemInfo:musicUsage*1024*1024];
});
});
}
/**
* 获取系统照片所占空间大小
*/
- (void)getSystemVideoSize
{
__block unsignedlong long photosSize = 0;
ALAssetsLibrary *library=[[ALAssetsLibraryalloc]init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result,NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[resultvalueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypeVideo]) {
// [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
photosSize += [[result defaultRepresentation]size];
NSLog(@"stop is %d",stop);
NSLog(@"getSystemVideoSize : %@",[selfformattedSystemInfo:photosSize]);
}
}
};
void (^ assetGroupEnumerator)( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group,BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {
NSLog(@"There is an error");
}];
}
/**
* 获取系统照片所占空间大小
*/
- (void)getSystemPhotosSize
{
__block unsignedlong long photosSize = 0;
ALAssetsLibrary *library=[[ALAssetsLibraryalloc]init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result,NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[resultvalueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypePhoto]) {
photosSize += [[result defaultRepresentation]size];
NSLog(@"getSystemPhotosSize : %@",[selfformattedSystemInfo:photosSize]);
}
} else {
NSLog(@"结束了 呆");
}
};
void (^ assetGroupEnumerator)( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group,BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {
NSLog(@"There is an error");
}];
}
/**
* 格式化系统所占空间大小(应用、照片、音乐、视频)
*/
- (NSString *)formattedSystemInfo:(unsignedlong long)size
{
NSString *formattedStr = nil;
if (size == 0) {
formattedStr = @"0.0 MB";
} else if (size >0 && size < 1024) {
formattedStr = [NSString stringWithFormat:@"%qu B", size];
} else if (size >=1024 && size < pow(1024,2)){
formattedStr = [NSString stringWithFormat:@"%.0f KB", (size / 1024.)];
} else if (size >=pow(1024, 2) && size <pow(1024, 3)) {
formattedStr = [NSString stringWithFormat:@"%.0f MB", (size / pow(1024, 2))];
} else if (size >=pow(1024, 3)) {
formattedStr = [NSString stringWithFormat:@"%.1f GB", (size / pow(1024, 3))];
}
return formattedStr;
}