1

我从 imageGallery 中检索了一张图片。现在我想知道图像的文件大小,以 KB 为单位。我该怎么做呢?请指导我。有可能找到吗?

4

3 回答 3

9

是的,你可以得到尺寸。试试下面的代码:

NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((image), 0.5)];
int imageSize   = imgData.length;
NSLog(@"size of image in KB: %f ", imageSize/1024.0);
于 2013-01-09T10:23:14.783 回答
1
    BOOL isPNG = [[yourImgPath lowercaseString] isEqualToString:@"png"];

    NSData *imageData = isPNG ?
    UIImagePNGRepresentation(img) :
    UIImageJPEGRepresentation(img, 0);

    int imageSize   = imageData.length;
    NSLog(@"size of image in KB: %f ", imageSize/1024.0);
于 2013-01-09T10:27:26.037 回答
1

试试这个:

NSString *imagePath=[[NSBundle mainBundle]pathForResource:@"images" ofType:@"jpeg"];
UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];

NSError *error;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:imagePath error:&error];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
NSLog(@"size of image is : %fKB ", fileSize/1024.0);
于 2013-01-09T10:34:21.070 回答