美文网首页iOS技术点iOS CollectioniOS开发
iOS微信聊天,朋友圈图片压缩算法

iOS微信聊天,朋友圈图片压缩算法

作者: 空有人心遮半面丶 | 来源:发表于2017-02-21 14:43 被阅读3217次

WXImageCompress

描述

图片作为App中重要的一个元素,非常具有表现力,图片既要让用户能看清楚,又能让发布图片的用户能快速的上传。所以开发者要对图片进行裁切和质量压缩。但是裁切尺寸质量压缩比设置成多少却很难控制好,如果设置不当会导致图片显示效果很差。

微信是一个很好的参照物,被大家广为使用并接受。这个扩展就是通过发送微信朋友圈和聊天会话发送了大量图片,对比原图与微信压缩后的图片逆向推算出来的压缩算法。


策略算法

图片尺寸

  • 宽高均 <= 1280,图片尺寸大小保持不变
  • 宽或高 > 1280 && 宽高比 <= 2,取较大值等于1280,较小值等比例压缩
  • 宽或高 > 1280 && 宽高比 > 2 && 宽或高 < 1280,图片尺寸大小保持不变
  • 宽高均 > 1280 && 宽高比 > 2,取较小值等于1280,较大值等比例压缩
注:当宽和高均小于1280,并且宽高比大于2时,微信聊天会话和微信朋友圈的处理不一样。
朋友圈:取较小值等于1280,较大值等比例压缩
聊天会话:取较小值等于800,较大值等比例压缩

图片质量

经过大量的测试,微信的图片压缩质量值 ≈0.5

UIImageJPEGRepresentation(resizeImage, 0.5)!

效果对比

original wechat this
1500 * 4000, 2.5MB 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.8MB 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.67MB 1280 * 890, 428KB 1280 * 891, 465KB

相关文章

网友评论

  • d3473952fba2:- (NSData *)wxImageSize:(UIImage *)image
    {
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeight = image.size.height;

    CGFloat boundary = 1280;

    NSData *data=UIImageJPEGRepresentation(image, 1.0);

    // width, height <= 1280, Size remains the same
    if (imageWidth <= boundary && imageHeight <= boundary) {
    UIImage* reImage = [self resizedImage:imageWidth withHeight:imageHeight withImage:image];
    data = UIImageJPEGRepresentation(reImage, 0.5);
    return data;
    }

    // aspect ratio
    CGFloat s = MAX(imageWidth, imageHeight) / MIN(imageWidth, imageHeight);

    if (s <= 2) {
    // Set the larger value to the boundary, the smaller the value of the compression
    CGFloat x = MAX(imageWidth, imageHeight) / boundary;
    if (imageWidth > imageHeight) {
    imageWidth = boundary ;
    imageHeight = imageHeight / x;
    }else{
    imageHeight = boundary;
    imageWidth = imageWidth / x;
    }
    }else{
    if (MIN(imageWidth, imageHeight) >= boundary) {
    //- parameter type: session image boundary is 800, timeline is 1280
    // boundary = type == .session ? 800 : 1280
    CGFloat x = MIN(imageWidth, imageHeight) / boundary;
    if (imageWidth < imageHeight) {
    imageWidth = boundary;
    imageHeight = imageHeight / x;
    } else {
    imageHeight = boundary;
    imageWidth = imageWidth / x;
    }
    }
    }
    UIImage* reImage = [self resizedImage:imageWidth withHeight:imageHeight withImage:image];
    data = UIImageJPEGRepresentation(reImage, 0.5);
    return data;
    }
    oc版。。copy楼主代码。。
    d3473952fba2:@xudehuai001 我是按照楼主的swift写的oc。。
    xudehuai001:@天空在蓝有什么用 使用该方法,图片尺寸会改变,但图片内容有可能会被拉伸,图片原宽高比也有可能改变,微信的图片好像都是保持原比例的吧
    d3473952fba2:- (UIImage *)resizedImage:(CGFloat)imageWidth withHeight:(CGFloat)imageHeight withImage:(UIImage *)image
    {
    CGRect newRect = CGRectMake(0, 0, imageWidth, imageHeight);
    UIImage *newImage;
    UIGraphicsBeginImageContext(newRect.size);
    newImage = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:image.imageOrientation];
    [newImage drawInRect:newRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
    }

本文标题:iOS微信聊天,朋友圈图片压缩算法

本文链接:https://www.haomeiwen.com/subject/kpkuwttx.html