Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

压缩图片质量

NSData *data = [WDCompressImage compressImageQuality:image toByte:204800];

//压缩图片质量
+ (NSData *)compressImageQuality:(UIImage *)image toByte:(NSInteger)maxLength {
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(image, compression);
    if (data.length < maxLength) return data;
    CGFloat max = 1;
    CGFloat min = 0;
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(image, compression);
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
    return data;
}

 

posted on 2017-11-20 11:23  Sinner_Yun  阅读(249)  评论(0编辑  收藏  举报

导航