如何在Swift中获取图像文件大小? [英] How to get image file size in Swift?

查看:1101
本文介绍了如何在Swift中获取图像文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIPopoverControllerDelegate

这些代表从我的画廊或相机中选择图像。那么,如何在选择图像后获得图像文件大小?

these delegates for choosing image from my gallery or my camera. So, how can I get image file size after choosing an image?

我想使用它:

let filePath = "your path here"
    var fileSize : UInt64 = 0

    do {
        let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(filePath)

        if let _attr = attr {
            fileSize = _attr.fileSize();
            print(fileSize)
        }
    } catch {
    }

但是这里我需要一个路径,但是如何在没有路径的情况下,仅通过图像文件?

but here I need a path, but how can I get without a path, just by image file?

推荐答案

  var image = info[UIImagePickerControllerOriginalImage] as! UIImage
    var imgData: NSData = NSData(data: UIImageJPEGRepresentation((image), 1)) 
// var imgData: NSData = UIImagePNGRepresentation(image) 
// you can also replace UIImageJPEGRepresentation with UIImagePNGRepresentation.
    var imageSize: Int = imgData.length
    print("size of image in KB: %f ", Double(imageSize) / 1024.0)

这篇关于如何在Swift中获取图像文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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