2017-04-11 173 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)") 
     }