2017-04-11 171 views
0

我有一個圖像(captureImage)這是由相機和調整大小。我使用UIImageJPEGRepresentation來壓縮圖像,壓縮後圖像的大小增加。這是正常行爲還是我以錯誤的方式進行壓縮/調整大小?UIImageJPEGRepresentation增加UIImage大小

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) 
} 
+1

請參考吼叫鏈接: http://stackoverflow.com/questions/29137488/how-do-i-resize-the-uiimage-to-reduce-upload-image-size可能對你有幫助。 – Aashish1aug

+0

您正在調整圖片大小:https://iosdevcenters.blogspot.com/2015/12/how-to-resize-image-in-swift-in-ios.html –

回答

0

是這個代碼的圖像壓縮得到該文件的大小

let compressedImageData = UIImageJPEGRepresentation(image, 0.3)! 

一種方式是通過保存到文件目錄。 例如 -

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)") 
     }