2019-11-07 3:空间大小

多家资源

如果 看到了是你的 我可以删除   ----

谢谢

//- (NSString *)freeDiskSpaceInBytes

//{

//    struct statfs buf;

//    long long freespace = -1;

//    if(statfs("/var", &buf) >= 0){

//        freespace = (long long)(buf.f_bsize * buf.f_bfree);

//    }

//    return [self humanReadableStringFromBytes:freespace];

//}

// 计算文件大小

//- (NSString *)humanReadableStringFromBytes:(unsigned long long)byteCount

//{

//    float numberOfBytes = byteCount;

//    int multiplyFactor = 0;

//

//    NSArray *tokens = [NSArray arrayWithObjects:@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB",@"EB",@"ZB",@"YB",nil];

//

//    while (numberOfBytes > 1024) {

//        numberOfBytes /= 1024;

//        multiplyFactor++;

//    }

//

//    return [NSString stringWithFormat:@"%4.2f %@",numberOfBytes, [tokens objectAtIndex:multiplyFactor]];

//}

//- (CGFloat)deviceFreeDisk{

//    NSError *error = nil;

//    NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];

//    if (error) return 0;

//    int64_t space =  [[attrs objectForKey:NSFileSystemFreeSize] longLongValue];

//    if (space < 0) space = 0;

//    return space/(1024.0*1024.0);

//}

//- (float)getFreeDiskspace{

//    float totalSpace;

//    float totalFreeSpace=0.f;

//    NSError *error = nil;

//    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

//    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];

//    if (dictionary) {

//        NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];

//        NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];

//        totalSpace = [fileSystemSizeInBytes floatValue];

//        totalFreeSpace = [freeFileSystemSizeInBytes floatValue];

//    }

//    return totalFreeSpace;

//}

//

//-(float)getTotalDiskSpaceInBytes {

//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//    struct statfs tStats;

//    statfs([[paths lastObject] cString], &tStats);

//    float totalSpace = (float)(tStats.f_blocks * tStats.f_bsize);

//    return totalSpace;

//}



// 空余硬盘大小

- (NSString *)freeDiskSpaceInBytes{


    CGFloattotalFreeSpace =0.0;

    NSError*error =nil;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];


    if(dictionary) {

        NSNumber*freeFileSystemSizeInBytes = [dictionaryobjectForKey:NSFileSystemFreeSize];

        totalFreeSpace = [freeFileSystemSizeInBytesfloatValue];

        NSLog(@" %f GB Free memory available.", ((totalFreeSpace/1024.f)/1024.f)/1024.f);

    }else{

        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);

    }


    if(@available(iOS11.0, *)) {

        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()];

        NSDictionary *results = [fileURL resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];

        if(!results) {

            NSLog(@"Error retrieving resource keys: %@%@",[error localizedDescription], [error userInfo]);

            abort();


        }

        NSLog(@"Available capacity for important usage: %lf",[[results objectForKey:NSURLVolumeAvailableCapacityForImportantUsageKey] floatValue]);

        totalFreeSpace = [[resultsobjectForKey:NSURLVolumeAvailableCapacityForImportantUsageKey] floatValue];

    }else{


    }

    return[selffileSizeToString:totalFreeSpace];

}

// 总共硬盘大小

- (NSString *)totalDiskSpace{


    CGFloattotalSpace =0.0;

    NSError*error =nil;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];

    if(dictionary) {

        NSNumber*fileSystemSizeInBytes = [dictionaryobjectForKey:NSFileSystemSize];

        totalSpace = [fileSystemSizeInBytesfloatValue];

        NSLog(@"Memory Capacity of %f GB .", ((totalSpace/1024.f)/1024.f/1024.f));

    }else{

        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);

    }

//    return totalSpace;

    return[selffileSizeToString:totalSpace];

}

-(NSString*)fileSizeToString:(CGFloat)fileSize

{

       NSIntegerKB;

    if(@available(iOS11, *)) {

        KB =1000.f;

    }else{

        KB =1024.f;

    }


       NSIntegerMB = KB*KB;

       NSIntegerGB = MB*KB;


       if(fileSize <10.f)  {

               return@"0 B";

            }elseif(fileSize < KB)    {

                    return@"< 1 KB";

                }elseif(fileSize < MB)    {

                        return[NSStringstringWithFormat:@"%.1f KB",((float)fileSize)/KB];

                    }elseif(fileSize < GB)    {

                            return[NSStringstringWithFormat:@"%.1f MB",((float)fileSize)/MB];

                        }else  {

                                 return[NSStringstringWithFormat:@"%.1f GB",((float)fileSize)/GB];

                            }

}

#pragma mark  空间剩余

-(void)spaceFreeAndTotal{


    NSString* str = [self freeDiskSpaceInBytes] ;

    NSString*  str1 = [selftotalDiskSpace] ;

    totalpacestr  = str1;

    freespacestr  = str ;

   NSString* sizeStr = [NSStringstringWithFormat:@"手机存储:总空间 %@/剩余%@可用",str1,str];

    showLabelStr= sizeStr;

    NSMutableAttributedString *strnn = [[NSMutableAttributedString alloc] initWithString:sizeStr];

    [strnnaddAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(9,totalpacestr.length)];

    NSIntegerwhereInt =9+totalpacestr.length+3;

    [strnnaddAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(whereInt,freespacestr.length)];

    self.showFreeSpaceL.attributedText= strnn;

}

}

你可能感兴趣的:(2019-11-07 3:空间大小)