本文介绍了如何在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中获取图像文件大小?
发布评论