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

标签 ios swift uiimageview uiimagepickercontroller

我正在使用

UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIPopoverControllerDelegate

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

我想用这个:

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 {
    }

但是这里我需要一个路径,但是没有路径怎么办,就靠图片文件呢?

最佳答案

请在 google 上查 1 kb 到 bytes 它将是 1000。

https://www.google.com/search?q=1+kb+%3D+how+many+bytes&oq=1+kb+%3D+how+many+bytes&aqs=chrome..69i57.8999j0j1&sourceid=chrome&ie=UTF-8


因此,在获得适当大小的同时,我通过在 App Bundle 中添加图像和在模拟器中的照片中添加了多个场景。 我从我的 Mac 上截取的图像是 299.0 KB。


场景 1:将图像添加到应用程序包

在您的 Xcode 中添加图像时,图像的大小将在项目目录中保持不变。但是你从它的路径中得到它的大小将减少到 257.0 KB。这是设备或模拟器中使用的图像的实际大小。

    guard let aStrUrl = Bundle.main.path(forResource: "1", ofType: "png") else { return }

   let aUrl = URL(fileURLWithPath: aStrUrl)
   print("Img size = \((Double(aUrl.fileSize) / 1000.00).rounded()) KB")

   extension URL {
        var attributes: [FileAttributeKey : Any]? {
            do {
                return try FileManager.default.attributesOfItem(atPath: path)
            } catch let error as NSError {
                print("FileAttribute error: \(error)")
            }
            return nil
        }

        var fileSize: UInt64 {
            return attributes?[.size] as? UInt64 ?? UInt64(0)
        }

        var fileSizeString: String {
            return ByteCountFormatter.string(fromByteCount: Int64(fileSize), countStyle: .file)
        }

        var creationDate: Date? {
            return attributes?[.creationDate] as? Date
        }
    }

场景 2:在模拟器中将图像添加到照片

将图像添加到模拟器或设备中的照片时,图像的大小从 299.0 KB 增加到 393.0 KB。这是存储在设备或模拟器文档目录中的图像的实际大小。

Swift 4 及更早版本

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.count
print("size of image in KB: %f ", Double(imageSize) / 1000.0)

swift 5

let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

let imgData = NSData(data: image.jpegData(compressionQuality: 1)!)
var imageSize: Int = imgData.count
print("actual size of image in KB: %f ", Double(imageSize) / 1000.0)   

通过添加 .rounded() 会得到 393.0 KB,不使用它会得到 393.442 KB。所以请使用上面的代码手动检查一次图像大小。由于图像的大小在不同的设备和 mac 中可能会有所不同。我只在 mac mini 和模拟器 iPhone XS 上检查过。

关于ios - 如何在 Swift 中获取图像文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34262791/

相关文章:

ios - 无法捕获 iOS 上从 "upsidedown"到横向的方向变化

ios - 2018 年 4 月,iOS 11 和新应用提交

ios - UITableView Cells - 如何使一个单元格的高度固定,其他单元格的高度相对于屏幕尺寸可变?

objective-c - 在 Swift 项目中使用 RSKImageCropper 库

ios - setImage 总是拾取 1x 图像

ios - 在 Parse.com 中,如何在 PFQueryTableViewController 中快速使用 query.includeKey?

ios - Swift 和 Objc 的互操作性——可用性不仅仅在 objc 中起作用

ios - 函数不改变变量(Swift)

ios - 在屏幕上移动 UIImage

iOS 一些 UIImageView 被拉伸(stretch)