如何在Swift中获取图像文件大小?

编程入门 行业动态 更新时间:2024-10-28 06:22:13
本文介绍了如何在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中获取图像文件大小?

本文发布于:2023-06-09 06:30:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/596879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件大小   图像   如何在   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!