0

I've an image (captureImage) which is taken by camera and resized. I use UIImageJPEGRepresentation to compress image, after the compression, image's size is increased. Is it normal behaviour or I'm doing compression/resizing in wrong way?

if let image = captureImage {
    print(image.size) // (700, 700)
    let compressedImageData = UIImageJPEGRepresentation(image, 0.3)!
    let compressedImage = UIImage(data: compressedImageData)!
    print(compressedImage.size) // (3024, 3024)
}
Jack
  • 13,571
  • 6
  • 76
  • 98
Rashid
  • 1,515
  • 16
  • 16

1 Answers1

0

Yes this code is for image compression

  let compressedImageData = UIImageJPEGRepresentation(image, 0.3)!

One way for getting the file size is by saving into document directory. For example -

  let filePath = fileURL
        var fileSize : UInt64

        do {
            //return [FileAttributeKey : Any]
            let attr = try FileManager.default.attributesOfItem(atPath: filePath.path)
            fileSize = attr[FileAttributeKey.size] as! UInt64

            //if you convert to NSDictionary, you can get file size old way as well.
            let dict = attr as NSDictionary
            fileSize = dict.fileSize()
            print(fileSize)

        } catch {
            print("Error: \(error)")
        }
Jack
  • 13,571
  • 6
  • 76
  • 98