WXImageCompress

describe

As an important element in the App, pictures are very expressive. They should not only make users see them clearly, but also allow users to upload them quickly. So developers have to crop and compress images. But the cutting size quality compression ratio is set to how much is difficult to control, if set improperly will lead to poor picture display effect.

Wechat is a good reference and is widely used and accepted. This extension is to send a large number of pictures by sending wechat moments and chat sessions, and then compare the original picture with the compressed picture of wechat and calculate the compression algorithm backwards.


Strategy algorithm

Image size

  • Width and height are all <= 1280, the size of the picture remains unchanged
  • Width or height > 1280&& width/height ratio <= 2, take the larger value equal to 1280, the smaller value equal proportion compression
  • Width or height > 1280&& width/height ratio > 2&& width or height < 1280, picture size remains the same
  • Width and height > 1280&& width and height ratio > 2, the smaller value is equal to 1280, and the larger value is compressed proportionally
Note: when width and height are less than1280And the width to height ratio is greater than2", wechat chat sessions and wechat moments are handled differently. Circle of friends: Take the smaller value equal to1280To compress the chat session proportionally: Take the smaller value equal to800, large value equal proportion compressionCopy the code

Image quality

After a lot of tests, the image compression quality value of wechat is about 0.5

UIImageJPEGRepresentation(resizeImage, 0.5)!Copy the code

Effect of contrast

original wechat this
1500 * 4000, 2.5 MB 800 * 2134, 325KB 800 * 2134, 306KB
960 * 600, 210KB 960 * 600, 147KB 960 * 600, 147KB
800 * 1280, 595KB 800 * 1280, 140KB 800 * 1280, 142KB
1080 * 1920, 1.8 MB 720 * 1280, 139KB 720 * 1280, 140KB
640 * 1136, 505KB 640 * 1136, 68KB 640 * 1136 69KB
4000 * 3000, 497KB 1280 * 960, 140KB 1280 * 960, 139KB
2560 * 1600, 232KB 1280 * 800 112KB 1280 * 800, 112KB
800 * 2138, 307KB 800 * 2134, 649KB 800 * 2138, 599KB
3351 * 1430, 386KB 1874 * 800, 296KB 1875 * 800, 286KB
3000 *1300, 458KB 1846 * 800 322KB 1847 * 800, 307KB
8323 * 5793, 19.67 MB 1280 * 890, 428KB 1280 * 891, 465KB