How to get the compressed file size of an image received from `UIImagePickerController`?

后端 未结 4 1447
再見小時候
再見小時候 2021-01-14 11:58

I want to know the size of image taken by UIImagePickerController by camara or library. Is there any way to find that ?

Requirement is like,

If

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 12:30

    If you have a path like jinx said, you could do this

    NSNumber *theFileSize = [NSNumber numberWithInt:0];
    NSFileManager * filemanager = [NSFileManager defaultManager];
    
    if([filemanager fileExistsAtPath:fullFilePath])
    {
        NSDictionary * attributes = [filemanager attributesOfItemAtPath:fullFilePath error:nil];
        theFileSize = [attributes objectForKey:NSFileSize];
    }
    
    [self convertbyteToKB_MB:[theFileSize  intValue];
    

    ...

    -(NSString*)convertbyteToKB_MB:(int)byte 
    {
    NSString    *retSize = [NSString stringWithFormat:@"%d bytes",byte];
    float       fByte = byte;
    if (byte > 1024)
    {
        //Kilobytes
        fByte = fByte / 1024;
        retSize = [NSString stringWithFormat:@"%.1f KB",fByte];
    }
    
    if (fByte > 1024)
    {
        //Megabytes
        fByte = fByte / 1024;
        retSize = [NSString stringWithFormat:@"%.1f MB",fByte]; 
    }
    
    return retSize;
    }
    

提交回复
热议问题