我研究的获取系统信息(应用、照片、音乐、视频)

本文介绍了一个iOS应用程序如何通过多线程技术获取系统中各种资源(包括应用、音乐、照片和视频)所占用的空间大小,并提供了详细的实现代码及格式化显示方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值