美文网首页
iOS图片压缩最佳方法和图片其他方法

iOS图片压缩最佳方法和图片其他方法

作者: 爱恨的潮汐 | 来源:发表于2018-05-24 13:07 被阅读266次

    图片压缩最佳方法和图片其他方法

    /**
     压缩图片方法(先压缩质量再压缩尺寸,压缩到指定尺寸以下单位如:1 * 1024 Kb)-最佳方法,只能用对象方法,否则无效.
     
     @param maxLength 压缩到指定质量以下,单位KB
     @return 压缩后的
     */
    -(NSData *)compressWithLengthLimit:(NSUInteger)maxLength{
        // Compress by quality
        CGFloat compression = 1;
        NSData *data = UIImageJPEGRepresentation(self, compression);
        //NSLog(@"Before compressing quality, image size = %ld KB",data.length/1024);
        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(self, compression);
            //NSLog(@"Compression = %.1f", compression);
            //NSLog(@"In compressing quality loop, image size = %ld KB", data.length / 1024);
            if (data.length < maxLength * 0.9) {
                min = compression;
            } else if (data.length > maxLength) {
                max = compression;
            } else {
                break;
            }
        }
        //NSLog(@"After compressing quality, image size = %ld KB", data.length / 1024);
        if (data.length < maxLength) return data;
        UIImage *resultImage = [UIImage imageWithData:data];
        // Compress by size
        NSUInteger lastDataLength = 0;
        while (data.length > maxLength && data.length != lastDataLength) {
            lastDataLength = data.length;
            CGFloat ratio = (CGFloat)maxLength / data.length;
            //NSLog(@"Ratio = %.1f", ratio);
            CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                                     (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
            UIGraphicsBeginImageContext(size);
            [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
            resultImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            data = UIImageJPEGRepresentation(resultImage, compression);
            //NSLog(@"In compressing size loop, image size = %ld KB", data.length / 1024);
        }
        //NSLog(@"After compressing size loop, image size = %ld KB", data.length / 1024);
        return data;
    }
    
    /**
     图片转成Base64字符串,并压缩到指定内存大小
     
     @param image 图片
     @param maxLength 压缩到指定大小,单位kb
     @return 字符串
     */
    -(NSString *)imageToBase64Str:(UIImage *) image  MaxLength:(NSInteger)maxLength{
       
        NSData *data = [self compressWithLengthLimit:maxLength * 1024.0f];
        
        NSLog(@"转Base64压缩后图片大小:%luk",(unsigned long)data.length/1024);
        NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        
        return encodedImageStr;
        
    }
    
    /**
     Base64字符串转图片
     
     @param encodedImageStr Base64字符串转
     @return 图片
     */
    +(UIImage *)Base64StrToUIImage:(NSString *)encodedImageStr{
        
        NSData * decodedImageData = [[NSData alloc] initWithBase64EncodedString:encodedImageStr options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
        UIImage * decodedImage = [UIImage imageWithData:decodedImageData];
        
        return decodedImage;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS图片压缩最佳方法和图片其他方法

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