Commit 24b6b8460178fa30032bd7e5f863b26d2530e45f

Authored by 梁星国
1 parent 1831e0ee

提交添加压缩文件

CNLiveUploadManager.podspec 0 → 100644
  1 +#
  2 +# Be sure to run `pod spec lint CNLiveUploadManager.podspec' to ensure this is a
  3 +# valid spec and to remove all comments including this before submitting the spec.
  4 +#
  5 +# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
  6 +# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
  7 +#
  8 +
  9 +Pod::Spec.new do |s|
  10 +
  11 + # ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  12 + #
  13 + # These will help people to find your library, and whilst it
  14 + # can feel like a chore to fill in it's definitely to your advantage. The
  15 + # summary should be tweet-length, and the description more in depth.
  16 + #
  17 +
  18 + s.name = "CNLiveUploadManager" # 项目名称
  19 + s.version = "0.0.1" # 版本号 与 你仓库的标签号 对应
  20 + s.summary = "图片,视频上传" # 项目简介
  21 +
  22 + # This description is used to generate tags and improve search results.
  23 + # * Think: What does it do? Why did you write it? What is the focus?
  24 + # * Try to keep it short, snappy and to the point.
  25 + # * Write the description between the DESC delimiters below.
  26 + # * Finally, don't worry about the indent, CocoaPods strips it!
  27 + s.description = <<-DESC
  28 +TODO:加入压缩测试
  29 + DESC
  30 +
  31 + s.homepage = "http://bj.gitlab.cnlive.com/ios-team/CNLiveUploadManager" # 仓库的主页
  32 +
  33 + # s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
  34 +
  35 +
  36 + # ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  37 + #
  38 + # Licensing your code is important. See http://choosealicense.com for more info.
  39 + # CocoaPods will detect a license file if there is a named LICENSE*
  40 + # Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  41 + #
  42 +
  43 + # s.license = "MIT (example)"
  44 + s.license = { :type => "MIT", :file => "FILE_LICENSE" }
  45 +
  46 +
  47 + # ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  48 + #
  49 + # Specify the authors of the library, with email addresses. Email addresses
  50 + # of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
  51 + # accepts just a name if you'd rather not provide an email address.
  52 + #
  53 + # Specify a social_media_url where others can refer to, for example a twitter
  54 + # profile URL.
  55 + #
  56 +
  57 + s.author = { "梁星国" => "liangxingguo_job@163.com" }
  58 + # Or just: s.author = "梁星国"
  59 + # s.authors = { "梁星国" => "jyyjkt@qq.com" }
  60 + # s.social_media_url = "http://twitter.com/梁星国"
  61 +
  62 + # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  63 + #
  64 + # If this Pod runs only on iOS or OS X, then specify the platform and
  65 + # the deployment target. You can optionally include the target after the platform.
  66 + #
  67 +
  68 + # s.platform = :ios
  69 + s.platform = :ios, "9.0"
  70 +
  71 + # When using multiple platforms
  72 + # s.ios.deployment_target = "5.0"
  73 + # s.osx.deployment_target = "10.7"
  74 + # s.watchos.deployment_target = "2.0"
  75 + # s.tvos.deployment_target = "9.0"
  76 +
  77 +
  78 + # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  79 + #
  80 + # Specify the location from where the source should be retrieved.
  81 + # Supports git, hg, bzr, svn and HTTP.
  82 + #
  83 +
  84 + s.source = { :git => "git@bj.gitlab.cnlive.com:ios-team/CNLiveUploadManager.git", :tag => "#{s.version}" }
  85 +
  86 +
  87 + # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  88 + #
  89 + # CocoaPods is smart about how it includes source code. For source files
  90 + # giving a folder will include any swift, h, m, mm, c & cpp files.
  91 + # For header files it will include any header in the folder.
  92 + # Not including the public_header_files will make all headers public.
  93 + #
  94 +
  95 + s.source_files = "CNLiveUploadManager/**/*" #文件路径
  96 + s.exclude_files = "Classes/Exclude"
  97 +
  98 + # s.public_header_files = "Classes/**/*.h"
  99 +
  100 +
  101 + # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  102 + #
  103 + # A list of resources included with the Pod. These are copied into the
  104 + # target bundle with a build phase script. Anything else will be cleaned.
  105 + # You can preserve files from being cleaned, please don't preserve
  106 + # non-essential files like tests, examples and documentation.
  107 + #
  108 +
  109 + # s.resource = "icon.png"
  110 + # s.resources = "Resources/*.png"
  111 +
  112 + # s.preserve_paths = "FilesToSave", "MoreFilesToSave"
  113 +
  114 +
  115 + # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  116 + #
  117 + # Link your library with frameworks, or libraries. Libraries do not include
  118 + # the lib prefix of their name.
  119 + #
  120 +
  121 + # s.framework = "SomeFramework"
  122 + s.frameworks = "UIKit", "Foundation"
  123 +
  124 + # s.library = "iconv"
  125 + # s.libraries = "iconv", "xml2"
  126 +
  127 +
  128 + # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  129 + #
  130 + # If your library depends on compiler flags you can set them in the xcconfig hash
  131 + # where they will only apply to your library. If you depend on other Podspecs
  132 + # you can include multiple dependencies to ensure it works.
  133 +
  134 + # s.requires_arc = true
  135 +
  136 + # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  137 + # s.dependency "JSONKit", "~> 1.4"
  138 +
  139 +end
CNLiveUploadManager/压缩图片/UIImage+Luban_iOS_Extension_h.h 0 → 100755
  1 +//
  2 +// UIImage+Luban_iOS_Extension_h.h
  3 +// Luban-iOS
  4 +//
  5 +// Created by guo on 2017/7/20.
  6 +// Copyright © 2017年 guo. All rights reserved.
  7 +//
  8 +
  9 +#import <UIKit/UIKit.h>
  10 +
  11 +@interface UIImage (Luban_iOS_Extension_h)
  12 +
  13 ++ (NSData *)lubanCompressImage:(UIImage *)image;//压缩图片
  14 ++ (NSData *)lubanCompressImage:(UIImage *)image withMask:(NSString *)maskName;//水印
  15 ++ (NSData *)lubanCompressImage:(UIImage *)image withCustomImage:(NSString *)imageName;//自定义名字压缩
  16 +
  17 +@end
CNLiveUploadManager/压缩图片/UIImage+Luban_iOS_Extension_h.m 0 → 100755
  1 +//
  2 +// UIImage+Luban_iOS_Extension_h.m
  3 +// Luban-iOS
  4 +//
  5 +// Created by guo on 2017/7/20.
  6 +// Copyright © 2017年 guo. All rights reserved.
  7 +//
  8 +
  9 +#import <objc/runtime.h>
  10 +#import "UIImage+Luban_iOS_Extension_h.h"
  11 +
  12 +@implementation UIImage (Luban_iOS_Extension_h)
  13 +
  14 +static char isCustomImage;
  15 +static char customImageName;
  16 +
  17 ++ (NSData *)lubanCompressImage:(UIImage *)image {
  18 + return [self lubanCompressImage:image withMask:nil];
  19 +}
  20 ++ (NSData *)lubanCompressImage:(UIImage *)image withMask:(NSString *)maskName {
  21 +
  22 + double size;
  23 + NSData *imageData = UIImageJPEGRepresentation(image, 1);
  24 +
  25 + NSLog(@"Luban-iOS image data size before compressed == %f Kb",imageData.length/1024.0);
  26 +
  27 + int fixelW = (int)image.size.width;
  28 + int fixelH = (int)image.size.height;
  29 + int thumbW = fixelW % 2 == 1 ? fixelW + 1 : fixelW;
  30 + int thumbH = fixelH % 2 == 1 ? fixelH + 1 : fixelH;
  31 +
  32 + double scale = ((double)fixelW/(double)fixelH);
  33 +
  34 + if (scale <= 1 && scale > 0.5625) {
  35 +
  36 + if (fixelH < 1664) {
  37 + if (imageData.length/1024.0 < 150) {
  38 + return imageData;
  39 + }
  40 + size = (fixelW * fixelH) / pow(1664, 2) * 150;
  41 + size = size < 60 ? 60 : size;
  42 + }
  43 + else if (fixelH >= 1664 && fixelH < 4990) {
  44 + thumbW = fixelW / 2;
  45 + thumbH = fixelH / 2;
  46 + size = (thumbH * thumbW) / pow(2495, 2) * 300;
  47 + size = size < 60 ? 60 : size;
  48 + }
  49 + else if (fixelH >= 4990 && fixelH < 10240) {
  50 + thumbW = fixelW / 4;
  51 + thumbH = fixelH / 4;
  52 + size = (thumbW * thumbH) / pow(2560, 2) * 300;
  53 + size = size < 100 ? 100 : size;
  54 + }
  55 + else {
  56 + int multiple = fixelH / 1280 == 0 ? 1 : fixelH / 1280;
  57 + thumbW = fixelW / multiple;
  58 + thumbH = fixelH / multiple;
  59 + size = (thumbW * thumbH) / pow(2560, 2) * 300;
  60 + size = size < 100 ? 100 : size;
  61 + }
  62 + }
  63 + else if (scale <= 0.5625 && scale > 0.5) {
  64 +
  65 + if (fixelH < 1280 && imageData.length/1024 < 200) {
  66 +
  67 + return imageData;
  68 + }
  69 + int multiple = fixelH / 1280 == 0 ? 1 : fixelH / 1280;
  70 + thumbW = fixelW / multiple;
  71 + thumbH = fixelH / multiple;
  72 + size = (thumbW * thumbH) / (1440.0 * 2560.0) * 400;
  73 + size = size < 100 ? 100 : size;
  74 + }
  75 + else {
  76 + int multiple = (int)ceil(fixelH / (1280.0 / scale));
  77 + thumbW = fixelW / multiple;
  78 + thumbH = fixelH / multiple;
  79 + size = ((thumbW * thumbH) / (1280.0 * (1280 / scale))) * 500;
  80 + size = size < 100 ? 100 : size;
  81 + }
  82 + return [self compressWithImage:image thumbW:thumbW thumbH:thumbH size:size withMask:maskName];
  83 +}
  84 +
  85 ++ (NSData *)lubanCompressImage:(UIImage *)image withCustomImage:(NSString *)imageName {
  86 +
  87 + if (imageName) {
  88 + objc_setAssociatedObject(self, &isCustomImage, @(1), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  89 + objc_setAssociatedObject(self, &customImageName, imageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  90 + }
  91 + return [self lubanCompressImage:image withMask:nil];
  92 +}
  93 +
  94 ++ (NSData *)compressWithImage:(UIImage *)image thumbW:(int)width thumbH:(int)height size:(double)size withMask:(NSString *)maskName {
  95 +
  96 + UIImage *thumbImage = [image fixOrientation];
  97 + thumbImage = [thumbImage resizeImage:image thumbWidth:width thumbHeight:height withMask:maskName];
  98 +
  99 + float qualityCompress = 0.0;
  100 + NSData *imageData = UIImageJPEGRepresentation(thumbImage, qualityCompress);
  101 +
  102 + NSUInteger lenght = imageData.length;
  103 + while (lenght / 1024 > size && qualityCompress <= (1-0.06)) {
  104 +
  105 + qualityCompress += 0.06 ;
  106 + int intCommpress = (int)(qualityCompress*100);
  107 + qualityCompress = intCommpress/100.0;
  108 + imageData = UIImageJPEGRepresentation(thumbImage, qualityCompress);
  109 + lenght = imageData.length;
  110 + thumbImage = [UIImage imageWithData:imageData];
  111 + }
  112 + NSLog(@"Luban-iOS image data size after compressed ==%f kb",imageData.length/1024.0);
  113 + return imageData;
  114 +}
  115 +
  116 +// specify the size
  117 +- (UIImage *)resizeImage:(UIImage *)image thumbWidth:(int)width thumbHeight:(int)height withMask:(NSString *)maskName {
  118 +
  119 + int outW = (int)image.size.width;
  120 + int outH = (int)image.size.height;
  121 +
  122 + int inSampleSize = 1;
  123 +
  124 + if (outW > width || outH > height) {
  125 + int halfW = outW / 2;
  126 + int halfH = outH / 2;
  127 +
  128 + while ((halfH / inSampleSize) > height && (halfW / inSampleSize) > width) {
  129 + inSampleSize *= 2;
  130 + }
  131 + }
  132 +
  133 + // keep one decimal
  134 + int outWith = (int)((outW / (float) width)*10);
  135 + int outHeiht = (int)((outH / (float) height)*10);
  136 + float heightRatio = outHeiht/10.0;
  137 + float widthRatio = outWith/10.0;
  138 +
  139 + if (heightRatio > 1 || widthRatio > 1) {
  140 +
  141 + inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
  142 + }
  143 + CGSize thumbSize = CGSizeMake((NSUInteger)((CGFloat)(outW/inSampleSize)), (NSUInteger)((CGFloat)(outH/inSampleSize)));
  144 +
  145 + UIGraphicsBeginImageContext(thumbSize);
  146 +
  147 + CGContextRef context = UIGraphicsGetCurrentContext();
  148 +
  149 + [image drawInRect:CGRectMake(0, 0, thumbSize.width, thumbSize.height)];
  150 + if (maskName) {
  151 +
  152 + CGContextTranslateCTM (context, thumbSize.width / 2, thumbSize.height / 2);
  153 + CGContextScaleCTM (context, 1, -1);
  154 +
  155 + [self drawMaskWithString:maskName context:context radius:0 angle:0 colour:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5] font:[UIFont systemFontOfSize:38.0] slantAngle:(CGFloat)(M_PI/6) size:thumbSize];
  156 + }
  157 + else {
  158 + NSNumber *iscustom = objc_getAssociatedObject(self, &isCustomImage);
  159 + BOOL isCustom = [iscustom boolValue];
  160 + if (isCustom) {
  161 + NSString *imageName = objc_getAssociatedObject(self, &customImageName);
  162 + UIImage *imageMask = [UIImage imageNamed:imageName];
  163 + if (imageMask) {
  164 + [imageMask drawInRect:CGRectMake(0, 0, thumbSize.width, thumbSize.height)];
  165 + }
  166 + }
  167 + }
  168 + UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
  169 + UIGraphicsEndImageContext();
  170 +
  171 + objc_setAssociatedObject(self, &isCustomImage, @(NO), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  172 +
  173 + return resultImage;
  174 +}
  175 +
  176 +- (UIImage *)fixOrientation {
  177 +
  178 + // No-op if the orientation is already correct
  179 + if (self.imageOrientation == UIImageOrientationUp) return self;
  180 +
  181 + // We need to calculate the proper transformation to make the image upright.
  182 + // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
  183 + CGAffineTransform transform = CGAffineTransformIdentity;
  184 +
  185 + switch (self.imageOrientation) {
  186 + case UIImageOrientationDown:
  187 + case UIImageOrientationDownMirrored:
  188 + transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
  189 + transform = CGAffineTransformRotate(transform, M_PI);
  190 + break;
  191 +
  192 + case UIImageOrientationLeft:
  193 + case UIImageOrientationLeftMirrored:
  194 + transform = CGAffineTransformTranslate(transform, self.size.width, 0);
  195 + transform = CGAffineTransformRotate(transform, M_PI_2);
  196 + break;
  197 +
  198 + case UIImageOrientationRight:
  199 + case UIImageOrientationRightMirrored:
  200 + transform = CGAffineTransformTranslate(transform, 0, self.size.height);
  201 + transform = CGAffineTransformRotate(transform, -M_PI_2);
  202 + break;
  203 + case UIImageOrientationUp:
  204 + case UIImageOrientationUpMirrored:
  205 + break;
  206 + }
  207 +
  208 + switch (self.imageOrientation) {
  209 + case UIImageOrientationUpMirrored:
  210 + case UIImageOrientationDownMirrored:
  211 + transform = CGAffineTransformTranslate(transform, self.size.width, 0);
  212 + transform = CGAffineTransformScale(transform, -1, 1);
  213 + break;
  214 +
  215 + case UIImageOrientationLeftMirrored:
  216 + case UIImageOrientationRightMirrored:
  217 + transform = CGAffineTransformTranslate(transform, self.size.height, 0);
  218 + transform = CGAffineTransformScale(transform, -1, 1);
  219 + break;
  220 + case UIImageOrientationUp:
  221 + case UIImageOrientationDown:
  222 + case UIImageOrientationLeft:
  223 + case UIImageOrientationRight:
  224 + break;
  225 + }
  226 +
  227 + // Now we draw the underlying CGImage into a new context, applying the transform
  228 + // calculated above.
  229 + CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
  230 + CGImageGetBitsPerComponent(self.CGImage), 0,
  231 + CGImageGetColorSpace(self.CGImage),
  232 + CGImageGetBitmapInfo(self.CGImage));
  233 + CGContextConcatCTM(ctx, transform);
  234 + switch (self.imageOrientation) {
  235 + case UIImageOrientationLeft:
  236 + case UIImageOrientationLeftMirrored:
  237 + case UIImageOrientationRight:
  238 + case UIImageOrientationRightMirrored:
  239 + // Grr...
  240 + CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
  241 + break;
  242 +
  243 + default:
  244 + CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
  245 + break;
  246 + }
  247 +
  248 + // And now we just create a new UIImage from the drawing context
  249 + CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
  250 + UIImage *img = [UIImage imageWithCGImage:cgimg];
  251 + CGContextRelease(ctx);
  252 + CGImageRelease(cgimg);
  253 + return img;
  254 +}
  255 +
  256 +- (void) drawMaskWithString:(NSString *)str context:(CGContextRef)context radius:(CGFloat)radius angle:(CGFloat)angle colour:(UIColor *)colour font:(UIFont *)font slantAngle:(CGFloat)slantAngle size:(CGSize)size{
  257 + // *******************************************************
  258 + // This draws the String str centred at the position
  259 + // specified by the polar coordinates (r, theta)
  260 + // i.e. the x= r * cos(theta) y= r * sin(theta)
  261 + // and rotated by the angle slantAngle
  262 + // *******************************************************
  263 +
  264 + // Set the text attributes
  265 + NSDictionary *attributes = @{NSForegroundColorAttributeName:colour,
  266 + NSFontAttributeName:font};
  267 + // Save the context
  268 + CGContextSaveGState(context);
  269 + // Undo the inversion of the Y-axis (or the text goes backwards!)
  270 + CGContextScaleCTM(context, 1, -1);
  271 + // Move the origin to the centre of the text (negating the y-axis manually)
  272 + CGContextTranslateCTM(context, radius * cos(angle), -(radius * sin(angle)));
  273 + // Rotate the coordinate system
  274 + CGContextRotateCTM(context, -slantAngle);
  275 + // Calculate the width of the text
  276 + CGSize offset = [str sizeWithAttributes:attributes];
  277 + // Move the origin by half the size of the text
  278 + CGContextTranslateCTM (context, -offset.width / 2, -offset.height / 2); // Move the origin to the centre of the text (negating the y-axis manually)
  279 + // Draw the text
  280 +
  281 + NSInteger width = ceil(cos(slantAngle)*offset.width);
  282 + NSInteger height = ceil(sin(slantAngle)*offset.width);
  283 +
  284 + NSInteger row = size.height/(height+100.0);
  285 + NSInteger coloum = size.width/(width+100.0)>6?:6;
  286 + CGFloat xPoint = 0;
  287 + CGFloat yPoint = 0;
  288 + for (NSInteger index = 0; index < row*coloum; index++) {
  289 +
  290 + xPoint = (index%coloum) *(width+100.0)-[UIScreen mainScreen].bounds.size.width;
  291 + yPoint = (index/coloum) *(height+100.0);
  292 + [str drawAtPoint:CGPointMake(xPoint, yPoint) withAttributes:attributes];
  293 + xPoint += -[UIScreen mainScreen].bounds.size.width;
  294 + yPoint += -[UIScreen mainScreen].bounds.size.height;
  295 + [str drawAtPoint:CGPointMake(xPoint, yPoint) withAttributes:attributes];
  296 + }
  297 +
  298 + // Restore the context
  299 + CGContextRestoreGState(context);
  300 +}
  301 +
  302 +@end
CNLiveUploadManager/压缩图片/UIImage+zipData.h 0 → 100644
  1 +//
  2 +// UIImage+zipData.h
  3 +// Luban-iOS
  4 +//
  5 +// Created by 梁星国 on 2018/8/24.
  6 +// Copyright © 2018年 guo. All rights reserved.
  7 +//
  8 +
  9 +#import <UIKit/UIKit.h>
  10 +typedef NS_ENUM(NSUInteger, AppPhototLimitType) {
  11 + AppPhototLimitTypeFriendsCircle = 0,//朋友圈图片限制20M
  12 + AppPhototLimitTypeTarentoComment = 1,//达人评论图片限制10M
  13 + AppPhototLimitTypeSweepCode = 2,//扫码图片限制10M
  14 + AppPhototLimitTypeChat = 3,//聊天图片限制20M
  15 +};
  16 +
  17 +@interface UIImage (zipData)
  18 +
  19 +
  20 +/**
  21 + 以1280作参照图像尺寸剪裁
  22 +
  23 + @param sourceImage 图片
  24 + @return 压缩图片
  25 + */
  26 ++(UIImage *)zipScaleWithImage:(UIImage *)sourceImage;
  27 +
  28 +/**
  29 + 以1080作参照图像尺寸剪裁
  30 +
  31 + @param sourceImage 图片
  32 + @return 压缩图片
  33 + */
  34 ++(UIImage *)zipScale_1080_WithImage:(UIImage *)sourceImage;
  35 +
  36 +/**
  37 + 自定义参照尺寸剪裁
  38 +
  39 + @param sourceImage 图片
  40 + @param accordingSize 参照尺寸
  41 + @return 压缩图片
  42 + */
  43 ++(UIImage *)zipScaleWithImage:(UIImage *)sourceImage accordingSize:(NSInteger )accordingSize;
  44 +
  45 +/**
  46 + 普通图限制20M,极限设置19M,小于100KB不压缩
  47 + @param sourceImage 图片
  48 + @return 返回压缩图片
  49 + */
  50 ++(NSData *)zipNSDataWithImage:(UIImage *)sourceImage;
  51 +
  52 +/**
  53 + 原图限制20M,极限设置19M,小于19M不压缩
  54 + 普通图限制20M,极限设置19M,小于100KB不压缩
  55 + @param sourceImage 图片
  56 + @param isOriginal 是否原图
  57 + @return 返回压缩图片
  58 + */
  59 ++(NSData *)zipNSDataWithImage:(UIImage *)sourceImage isOriginal:(BOOL)isOriginal;
  60 +
  61 +/**
  62 + 以1280作参照图像尺寸压缩,质量压缩
  63 +
  64 + @param sourceImage 图片
  65 + @return 返回压缩结果
  66 + */
  67 ++(NSData *)zipNSDataAndScaleWithImage:(UIImage *)sourceImage;
  68 +
  69 +/**
  70 + 以1080作参照图像尺寸压缩,质量压缩
  71 +
  72 + @param sourceImage 图片
  73 + @return 返回压缩结果
  74 + */
  75 ++(NSData *)zip_1080_NSDataAndScaleWithImage:(UIImage *)sourceImage;
  76 +
  77 +
  78 +
  79 +@end
CNLiveUploadManager/压缩图片/UIImage+zipData.m 0 → 100644
  1 +//
  2 +// UIImage+zipData.m
  3 +// Luban-iOS
  4 +//
  5 +// Created by 梁星国 on 2018/8/24.
  6 +// Copyright © 2018年 guo. All rights reserved.
  7 +//
  8 +
  9 +#import "UIImage+zipData.h"
  10 +
  11 +@implementation UIImage (zipData)
  12 +
  13 +
  14 +/**
  15 + 以1280作参照图像尺寸压缩
  16 +
  17 + @param sourceImage 图片
  18 + @return 压缩图片
  19 + */
  20 ++(UIImage *)zipScaleWithImage:(UIImage *)sourceImage{
  21 + //进行图像尺寸的压缩
  22 + CGSize imageSize = sourceImage.size;//取出要压缩的image尺寸
  23 + CGFloat width = imageSize.width; //图片宽度
  24 + CGFloat height = imageSize.height; //图片高度
  25 + //1.宽高大于1280(宽高比不按照2来算,按照1来算)
  26 + if (width>1280||height>1280) {
  27 + if (width>height) {
  28 + CGFloat scale = height/width;
  29 + width = 1280;
  30 + height = width*scale;
  31 + }else{
  32 + CGFloat scale = width/height;
  33 + height = 1280;
  34 + width = height*scale;
  35 + }
  36 + //2.宽大于1280高小于1280
  37 + }else if(width>1280||height<1280){
  38 + CGFloat scale = height/width;
  39 + width = 1280;
  40 + height = width*scale;
  41 + //3.宽小于1280高大于1280
  42 + }else if(width<1280||height>1280){
  43 + CGFloat scale = width/height;
  44 + height = 1280;
  45 + width = height*scale;
  46 + //4.宽高都小于1280
  47 + }else{
  48 + //不缩放尺寸
  49 + }
  50 + //进行尺寸重绘
  51 + UIGraphicsBeginImageContext(CGSizeMake(width, height));
  52 + [sourceImage drawInRect:CGRectMake(0,0,width,height)];
  53 + UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  54 + UIGraphicsEndImageContext();
  55 + return newImage;
  56 +}
  57 +
  58 +/**
  59 + 以1080作参照图像尺寸压缩
  60 +
  61 + @param sourceImage 图片
  62 + @return 压缩图片
  63 + */
  64 ++(UIImage *)zipScale_1080_WithImage:(UIImage *)sourceImage{
  65 + //进行图像尺寸的压缩
  66 + CGSize imageSize = sourceImage.size;//取出要压缩的image尺寸
  67 + CGFloat width = imageSize.width; //图片宽度
  68 + CGFloat height = imageSize.height; //图片高度
  69 + //1.宽高大于1080(宽高比不按照2来算,按照1来算)
  70 + if (width>1080||height>1080) {
  71 + if (width>height) {
  72 + CGFloat scale = height/width;
  73 + width = 1080;
  74 + height = width*scale;
  75 + }else{
  76 + CGFloat scale = width/height;
  77 + height = 1080;
  78 + width = height*scale;
  79 + }
  80 + //2.宽大于1080高小于1080
  81 + }else if(width>1080||height<1280){
  82 + CGFloat scale = height/width;
  83 + width = 1080;
  84 + height = width*scale;
  85 + //3.宽小于1080高大于1080
  86 + }else if(width<1080||height>1080){
  87 + CGFloat scale = width/height;
  88 + height = 1080;
  89 + width = height*scale;
  90 + //4.宽高都小于1080
  91 + }else{
  92 + //不缩放尺寸
  93 + }
  94 + //进行尺寸重绘
  95 + UIGraphicsBeginImageContext(CGSizeMake(width, height));
  96 + [sourceImage drawInRect:CGRectMake(0,0,width,height)];
  97 + UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  98 + UIGraphicsEndImageContext();
  99 + return newImage;
  100 +}
  101 +
  102 +
  103 +/**
  104 + 自定义参照尺寸压缩
  105 +
  106 + @param sourceImage 图片
  107 + @param accordingSize 参照尺寸
  108 + @return 压缩图片
  109 + */
  110 ++(UIImage *)zipScaleWithImage:(UIImage *)sourceImage accordingSize:(NSInteger )accordingSize{
  111 + //进行图像尺寸的压缩
  112 + CGSize imageSize = sourceImage.size;//取出要压缩的image尺寸
  113 + CGFloat width = imageSize.width; //图片宽度
  114 + CGFloat height = imageSize.height; //图片高度
  115 + //1.宽高大于accordingSize(宽高比不按照2来算,按照1来算)
  116 + if (width>accordingSize||height>accordingSize) {
  117 + if (width>height) {
  118 + CGFloat scale = height/width;
  119 + width = accordingSize;
  120 + height = width*scale;
  121 + }else{
  122 + CGFloat scale = width/height;
  123 + height = accordingSize;
  124 + width = height*scale;
  125 + }
  126 + //2.宽大于accordingSize高小于accordingSize
  127 + }else if(width>accordingSize||height<accordingSize){
  128 + CGFloat scale = height/width;
  129 + width = accordingSize;
  130 + height = width*scale;
  131 + //3.宽小于accordingSize高大于accordingSize
  132 + }else if(width<accordingSize||height>accordingSize){
  133 + CGFloat scale = width/height;
  134 + height = accordingSize;
  135 + width = height*scale;
  136 + //4.宽高都小于accordingSize
  137 + }else{
  138 + //不缩放尺寸
  139 + }
  140 + //进行尺寸重绘
  141 + UIGraphicsBeginImageContext(CGSizeMake(width, height));
  142 + [sourceImage drawInRect:CGRectMake(0,0,width,height)];
  143 + UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  144 + UIGraphicsEndImageContext();
  145 + return newImage;
  146 +}
  147 +
  148 +/**
  149 + 普通图限制20M,极限设置19M,小于100KB不压缩
  150 + @param sourceImage 图片
  151 + @return 返回压缩图片
  152 + */
  153 ++(NSData *)zipNSDataWithImage:(UIImage *)sourceImage{
  154 + return [UIImage zipNSDataWithImage:sourceImage isOriginal:NO];
  155 +}
  156 +
  157 +
  158 +/**
  159 + 原图限制20M,极限设置19M,小于19M不压缩
  160 + 普通图限制20M,极限设置19M,小于100KB不压缩
  161 + @param sourceImage 图片
  162 + @param isOriginal 是否原图
  163 + @return 返回压缩图片
  164 + */
  165 ++(NSData *)zipNSDataWithImage:(UIImage *)sourceImage isOriginal:(BOOL)isOriginal{
  166 + NSLog(@"输出发送图片原始大小 == %f Kb",UIImageJPEGRepresentation(sourceImage, 1.0).length/1024.0);
  167 + //进行图像的画面质量压缩
  168 + NSData *data = UIImageJPEGRepresentation(sourceImage, 1.0);
  169 + if (isOriginal) {//原图
  170 + if (data.length > 19*1024*1024) {//19M以上
  171 + NSNumber * dataLength = [NSNumber numberWithInteger:data.length];
  172 + NSNumber * limitLength = [NSNumber numberWithInteger:19*1024*1024];
  173 + CGFloat scale = [limitLength floatValue]/ [dataLength floatValue];
  174 + scale = scale > 0.5 ? 0.5 : scale;
  175 + data = UIImageJPEGRepresentation(sourceImage, scale);
  176 + }else{
  177 + //不压缩
  178 + }
  179 + }else{
  180 + data = [UIImage zipNSDataAndScaleWithImage:sourceImage];
  181 + }
  182 +
  183 + NSLog(@"/***原图限制20M,极限设置19M,小于100KB不压缩输出图片大小 == %f Kb",data.length/1024.0);
  184 + return data;
  185 +}
  186 +
  187 +/**
  188 + 以1280作参照图像尺寸压缩,质量压缩
  189 +
  190 + @param sourceImage 图片
  191 + @return 返回压缩结果
  192 + */
  193 ++(NSData *)zipNSDataAndScaleWithImage:(UIImage *)sourceImage{
  194 + NSLog(@"输出发送图片原始大小 == %f Kb",UIImageJPEGRepresentation(sourceImage, 1.0).length/1024.0);
  195 + //进行图像尺寸的压缩
  196 + CGSize imageSize = sourceImage.size;//取出要压缩的image尺寸
  197 + CGFloat width = imageSize.width; //图片宽度
  198 + CGFloat height = imageSize.height; //图片高度
  199 + //1.宽高大于1280(宽高比不按照2来算,按照1来算)
  200 + if (width>1280||height>1280) {
  201 + if (width>height) {
  202 + CGFloat scale = height/width;
  203 + width = 1280;
  204 + height = width*scale;
  205 + }else{
  206 + CGFloat scale = width/height;
  207 + height = 1280;
  208 + width = height*scale;
  209 + }
  210 + //2.宽大于1280高小于1280
  211 + }else if(width>1280||height<1280){
  212 + CGFloat scale = height/width;
  213 + width = 1280;
  214 + height = width*scale;
  215 + //3.宽小于1280高大于1280
  216 + }else if(width<1280||height>1280){
  217 + CGFloat scale = width/height;
  218 + height = 1280;
  219 + width = height*scale;
  220 + //4.宽高都小于1280
  221 + }else{
  222 + }
  223 + UIGraphicsBeginImageContext(CGSizeMake(width, height));
  224 + [sourceImage drawInRect:CGRectMake(0,0,width,height)];
  225 + UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  226 + UIGraphicsEndImageContext();
  227 +
  228 + //进行图像的画面质量压缩
  229 + NSData *data=UIImageJPEGRepresentation(newImage, 1.0);
  230 + if (data.length>100*1024) {
  231 + if (data.length > 19*1024*1024) {//19M以上
  232 + NSNumber * dataLength = [NSNumber numberWithInteger:data.length];
  233 + NSNumber * limitLength = [NSNumber numberWithInteger:19*1024*1024];
  234 + CGFloat scale = [limitLength floatValue]/ [dataLength floatValue];
  235 + scale = scale > 0.3 ? 0.3 : scale;
  236 + data = UIImageJPEGRepresentation(newImage, scale);
  237 + }else if (data.length > 10*1024*1024){//10M<x<=19M
  238 + data = UIImageJPEGRepresentation(newImage, 0.5);
  239 + }else if (data.length > 1*1024*1024){//1M<x<=10M
  240 + data = UIImageJPEGRepresentation(newImage, 0.7);
  241 + }else if (data.length > 512*1024) {//0.5M<x<1M
  242 + data = UIImageJPEGRepresentation(newImage, 0.8);
  243 + }else if (data.length > 256*1024) {//0.1M-0.5M
  244 + data = UIImageJPEGRepresentation(newImage, 0.9);
  245 + }else{
  246 + //不压缩
  247 + }
  248 + }
  249 + NSLog(@"/***以1280作参照图像尺寸压缩,质量压缩输出图片大小 == %f Kb",data.length/1024.0);
  250 + return data;
  251 +}
  252 +
  253 +
  254 +/**
  255 + 以1080作参照图像尺寸压缩,质量压缩
  256 +
  257 + @param sourceImage 图片
  258 + @return 返回压缩结果
  259 + */
  260 ++(NSData *)zip_1080_NSDataAndScaleWithImage:(UIImage *)sourceImage{
  261 + NSLog(@"输出发送图片原始大小 == %f Kb",UIImageJPEGRepresentation(sourceImage, 1.0).length/1024.0);
  262 + //进行图像尺寸的压缩
  263 + CGSize imageSize = sourceImage.size;//取出要压缩的image尺寸
  264 + CGFloat width = imageSize.width; //图片宽度
  265 + CGFloat height = imageSize.height; //图片高度
  266 + //1.宽高大于1080(宽高比不按照2来算,按照1来算)
  267 + if (width>1080||height>1080) {
  268 + if (width>height) {
  269 + CGFloat scale = height/width;
  270 + width = 1080;
  271 + height = width*scale;
  272 + }else{
  273 + CGFloat scale = width/height;
  274 + height = 1080;
  275 + width = height*scale;
  276 + }
  277 + //2.宽大于1080高小于1080
  278 + }else if(width>1080||height<1080){
  279 + CGFloat scale = height/width;
  280 + width = 1080;
  281 + height = width*scale;
  282 + //3.宽小于1080高大于1080
  283 + }else if(width<1080||height>1080){
  284 + CGFloat scale = width/height;
  285 + height = 1080;
  286 + width = height*scale;
  287 + //4.宽高都小于1080
  288 + }else{
  289 + }
  290 + UIGraphicsBeginImageContext(CGSizeMake(width, height));
  291 + [sourceImage drawInRect:CGRectMake(0,0,width,height)];
  292 + UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  293 + UIGraphicsEndImageContext();
  294 +
  295 + //进行图像的画面质量压缩
  296 + NSData *data=UIImageJPEGRepresentation(newImage, 1.0);
  297 + if (data.length>100*1024) {
  298 + if (data.length > 19*1024*1024) {//19M以上
  299 + NSNumber * dataLength = [NSNumber numberWithInteger:data.length];
  300 + NSNumber * limitLength = [NSNumber numberWithInteger:19*1024*1024];
  301 + CGFloat scale = [limitLength floatValue]/ [dataLength floatValue];
  302 + scale = scale > 0.3 ? 0.3 : scale;
  303 + data = UIImageJPEGRepresentation(newImage, scale);
  304 + }else if (data.length > 10*1024*1024){//10M<x<=19M
  305 + data = UIImageJPEGRepresentation(newImage, 0.5);
  306 + }else if (data.length > 1*1024*1024){//1M<x<=10M
  307 + data = UIImageJPEGRepresentation(newImage, 0.7);
  308 + }else if (data.length > 512*1024) {//0.5M<x<1M
  309 + data = UIImageJPEGRepresentation(newImage, 0.8);
  310 + }else if (data.length > 256*1024) {//0.1M-0.5M
  311 + data = UIImageJPEGRepresentation(newImage, 0.9);
  312 + }else{
  313 + //不压缩
  314 + }
  315 + }
  316 + NSLog(@"/***以1080作参照图像尺寸压缩,质量压缩输出图片大小 == %f Kb",data.length/1024.0);
  317 + return data;
  318 +}
  319 +
  320 +
  321 +
  322 +@end