Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

swift4,压缩图片?

iOS项目中需要对用户上传的图片进行压缩

根据后台的能力,现在的设计是将所有过大的图片(大于1mb)压缩至1mb内

现在思路是,获取当前图片的大小,判断是否大于1024kb,若大于则压缩

但是就在获取图片的大小时遇到了问题

UIImage没发直接获取图片大小,需要先转成 Data 类型

 let data: Data = UIImageJPEGRepresentation(image!,0.5)!

 let imageSize: Float = Float(data.count/1024)
 let data: Data = UIImagePNGRepresentation(image!)!

 let imageSize: Float = Float(data.count/1024)

不论是 UIImageJPEGRepresentation 还是 UIImagePNGRepresentation 得出的尺寸都和图片本身的尺寸不相符。。。

不知道哪里出了问题

后期我也想通过图片原图大小和1024kb进行计算,得出压缩比,用UIImageJPEGRepresentation的方法压缩

但是实际测试,图片越压越大了。。。

两个问题百度了半天,希望得到解答


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...