美文网首页
iOS图片压缩方法

iOS图片压缩方法

作者: 馒头Show | 来源:发表于2017-05-22 17:08 被阅读0次

    #define KCompressibilityFactor 1280.00

    @implementationWeChatJPEGImager

    #pragma mark - 压缩一张图片 最大宽高1280 类似于微信算法

    - (UIImage*)getJPEGImagerImg:(UIImage*)image{

    CGFloat oldImg_WID = image.size.width;

    CGFloat oldImg_HEI = image.size.height;

    //CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比

    if(oldImg_WID > KCompressibilityFactor || oldImg_HEI > KCompressibilityFactor){

    //超过设置的最大宽度 先判断那个边最长

    if(oldImg_WID > oldImg_HEI){

    //宽度大于高度

    oldImg_HEI = (KCompressibilityFactor* oldImg_HEI)/oldImg_WID;

    oldImg_WID = KCompressibilityFactor;

    }else{

    oldImg_WID = (KCompressibilityFactor* oldImg_WID)/oldImg_HEI;

    oldImg_HEI = KCompressibilityFactor;

    }

    }

    UIImage*newImg = [selfimageWithImage:imagescaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];

    NSData*dJpeg =nil;

    if(UIImagePNGRepresentation(newImg)==nil) {

    dJpeg = UIImageJPEGRepresentation(newImg,0.5);

    }else{

    dJpeg = UIImagePNGRepresentation(newImg);

    }

    return[UIImageimageWithData:dJpeg];

    }

    #pragma mark - 压缩多张图片 最大宽高1280 类似于微信算法

    - (NSArray*)getJPEGImagerImgArr:(NSArray*)imageArr{

    NSMutableArray*newImgArr = [NSMutableArraynew];

    for(inti =0; i

    UIImage*newImg = [selfgetJPEGImagerImg:imageArr[i]];

    [newImgArraddObject:newImg];

    }

    returnnewImgArr;

    }

    #pragma mark - 压缩一张图片 自定义最大宽高

    - (UIImage*)getJPEGImagerImg:(UIImage*)imagecompressibilityFactor:(CGFloat)compressibilityFactor{

    CGFloat oldImg_WID = image.size.width;

    CGFloat oldImg_HEI = image.size.height;

    //CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比

    if(oldImg_WID > compressibilityFactor || oldImg_HEI > compressibilityFactor){

    //超过设置的最大宽度 先判断那个边最长

    if(oldImg_WID > oldImg_HEI){

    //宽度大于高度

    oldImg_HEI = (compressibilityFactor* oldImg_HEI)/oldImg_WID;

    oldImg_WID = compressibilityFactor;

    }else{

    oldImg_WID = (compressibilityFactor* oldImg_WID)/oldImg_HEI;

    oldImg_HEI = compressibilityFactor;

    }

    }

    UIImage*newImg = [selfimageWithImage:imagescaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];

    NSData*dJpeg =nil;

    if(UIImagePNGRepresentation(newImg)==nil) {

    dJpeg = UIImageJPEGRepresentation(newImg,0.5);

    }else{

    dJpeg = UIImagePNGRepresentation(newImg);

    }

    return[UIImageimageWithData:dJpeg];

    }

    #pragma mark - 压缩多张图片 自定义最大宽高

    - (NSArray*)getJPEGImagerImgArr:(NSArray*)imageArrcompressibilityFactor:(CGFloat)compressibilityFactor{

    NSMutableArray*newImgArr = [NSMutableArraynew];

    for(inti =0; i

    UIImage*newImg = [selfgetJPEGImagerImg:imageArr[i]compressibilityFactor:compressibilityFactor];

    [newImgArraddObject:newImg];

    }

    returnnewImgArr;

    }

    #pragma mark - 根据宽高压缩图片

    - (UIImage*)imageWithImage:(UIImage*)imagescaledToSize:(CGSize)newSize{

    UIGraphicsBeginImageContext(newSize);

    [imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    returnnewImage;

    }

    相关文章

      网友评论

          本文标题:iOS图片压缩方法

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