如何以KB为单位获取UIImage的大小? [英] How to get the size of a UIImage in KB?

查看:86
本文介绍了如何以KB为单位获取UIImage的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 UIImage 获取以 KB 为单位的文件大小,而无需从 didFinishPickingMediaWithInfo 获取该图像?呈现的图片来自相册.

Is there a way to get the filesize in KB from a UIImage, without getting that image from didFinishPickingMediaWithInfo? The images that are presented are coming from the photo album.

我尝试了以下代码,但结果如下:图像大小(以 KB 为单位):0.000000

I tried the following code, but this gives the following result: size of image in KB: 0.000000

- (void)setImage:(UIImage *)image
{
    _image = image;
    self.imageView.image = image;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];

        self.backgroundColor = [UIColor whiteColor];

        panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(beingDragged:)];
        [self addGestureRecognizer:panGestureRecognizer];

        // prepare image view
        self.imageView = [[UIImageView alloc]initWithFrame:self.bounds];
        self.imageView.clipsToBounds = YES;
        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:self.imageView];



        NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((_image), 0.5)];
        int imageSize = imgData.length;
        NSLog(@"size of image in KB: %f ", imageSize/1024.0);

        overlayView = [[OverlayView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-100, 0, 100, 100)];
        overlayView.alpha = 0;
        [self addSubview:overlayView];


    }
    return self;
}

推荐答案

以下是计算 HomeDirectory 或 Documents 中文件大小的示例:

Here is an example of calculating file sizes of files in your HomeDirectory or Documents:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"yourimagename.png"]

文件sie计算:

       filesize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] fileSize];

       NSLog(@"%lld",filesize);

在添加文件大小之前,您可以将其添加到 .m 文件中

Before you do that add filesize, you can add it in the .m file

@interface ViewController () {
long long filesize;
}

这将产生字节,如果您尝试将这些字节转换为 kb,您可以使用 NSByteCountFormatter,它会为您处理所有数学运算:

this will result in bytes, if you are trying to convert those bytes into kb you can use the NSByteCountFormatter and it will take care of all the math for you:

 NSByteCountFormatter *sizeFormatter = [[NSByteCountFormatter alloc] init];
sizeFormatter.countStyle = NSByteCountFormatterCountStyleFile;

然后像这样调用它:

[sizeFormatter stringFromByteCount:filesize]

如果图像没有保存在磁盘上,您可以这样计算大小:

If the image is not saved on the disk you can calculate the size this way:

NSData *imgData = UIImageJPEGRepresentation(_image, 1);
filesize = [imgData length]; //filesize in this case will be an int not long long so use %d to NSLog it

这篇关于如何以KB为单位获取UIImage的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆