如何从图像选择器获取KB或Mb的图像大小?

时间:2018-04-17 11:31:22

标签: ios swift uiimagepickercontroller uiimagejpegrepresentation

我面临一些问题,在我的代码中获取图像大小就是我正在做的事情:

let imageData : NSData = UIImageJPEGRepresentation(images[0], 1) as! NSData 
let formatter = ByteCountFormatter() 
formatter.allowedUnits = .useKB 
formatter.countStyle = .binary 
let imageSize = formatter.string(fromByteCount: Int64(imageData.length))
print(imageSize)

3 个答案:

答案 0 :(得分:0)

此处您可以获取图像尺寸

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            var Size = Float()
            var data = Data()
            btnCancel.isUserInteractionEnabled = true

            if let mediaType = info[UIImagePickerControllerMediaType] as? String {

                if mediaType  == "public.image" {
                    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
                        data = UIImageJPEGRepresentation(pickedImage, 1.0)!
                      //Here you get MB size
                        Size = Float(Double(data.count)/1024/1024)

                      //For Kb just remove single 1024 from size 

                      // I am checking 5 MB size here you check as you want
                        if Size <= 5.00{
                            // Here your image
                        }  
                    }
                }
                else
                if mediaType == "public.movie" {
                    let videoURL = info[UIImagePickerControllerMediaURL] as? URL
                    data = try! Data.init(contentsOf: videoURL!)
                    Size = Float(Double(data.count)/1024/1024)
                    if Size <= 5.00{
                        //your video here
                    }
                }
            }
            picker.dismiss(animated: true) {
                if Size > 5.0{
                    Utilities.showAlertView(title: "title", message: "message")
                }
            }
        }

        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            picker.dismiss(animated: true, completion: nil)
        }

答案 1 :(得分:0)

您只需按以下UIImage扩展名检查KB大小:

extension UIImage {
    func logImageSizeInKB(scale: CGFloat) -> (Int, Data) {
        let data = UIImageJPEGRepresentation(self, scale)!
        let formatter = ByteCountFormatter()
        formatter.allowedUnits = ByteCountFormatter.Units.useKB
        formatter.countStyle = ByteCountFormatter.CountStyle.file
        let imageSize = formatter.string(fromByteCount: Int64(data.count))
        print("ImageSize(KB): \(imageSize)")

        return (Int(Int64(data.count) / 1024), data)
    }
}

注意:

对于png图片大小,也可以使用UIImagePNGRepresentation。顺便说一句,PNG图像比JPEG / JPG重。

答案 2 :(得分:0)

尝试此代码,以便在从选择器视图中获取图像时获取图像大小。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        dismiss(animated: true, completion: nil);

        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        //get the size of image
        let imgData: NSData = NSData(data: UIImageJPEGRepresentation((chosenImage), 1)!)
        let imageSize: Int = imgData.length
        print("size of image in MB: %f ", Double(imageSize) / 1024.0/1024.0)
    }