Created
July 10, 2018 02:31
-
-
Save SongJiaqiang/fd860a25c66c39688ff41be9c1730493 to your computer and use it in GitHub Desktop.
ITCodeShare图片压缩到指定大小
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
作者:ITCodeShare | |
链接:https://www.jianshu.com/p/99c3e6a6c033 | |
來源:简书 | |
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。 | |
*/ | |
- (NSData *)compressQualityWithMaxLength:(NSInteger)maxLength { | |
CGFloat compression = 1; | |
NSData *data = UIImageJPEGRepresentation(self, compression); | |
while (data.length > maxLength && compression > 0) { | |
compression -= 0.02; | |
data = UIImageJPEGRepresentation(self, compression); // When compression less than a value, this code dose not work | |
} | |
return data; | |
} | |
-(NSData *)compressBySizeWithMaxLength:(NSUInteger)maxLength{ | |
UIImage *resultImage = self; | |
NSData *data = UIImageJPEGRepresentation(resultImage, 1); | |
NSUInteger lastDataLength = 0; | |
while (data.length > maxLength && data.length != lastDataLength) { | |
lastDataLength = data.length; | |
CGFloat ratio = (CGFloat)maxLength / data.length; | |
CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)), | |
(NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank | |
UIGraphicsBeginImageContext(size); | |
// Use image to draw (drawInRect:), image is larger but more compression time | |
// Use result image to draw, image is smaller but less compression time | |
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)]; | |
resultImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
data = UIImageJPEGRepresentation(resultImage, 1); | |
} | |
return data; | |
} | |
-(NSData *)compressBySizeWithMaxLength:(NSUInteger)maxLength{ | |
UIImage *resultImage = self; | |
NSData *data = UIImageJPEGRepresentation(resultImage, 1); | |
NSUInteger lastDataLength = 0; | |
while (data.length > maxLength && data.length != lastDataLength) { | |
lastDataLength = data.length; | |
CGFloat ratio = (CGFloat)maxLength / data.length; | |
CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)), | |
(NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank | |
UIGraphicsBeginImageContext(size); | |
// Use image to draw (drawInRect:), image is larger but more compression time | |
// Use result image to draw, image is smaller but less compression time | |
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)]; | |
resultImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
data = UIImageJPEGRepresentation(resultImage, 1); | |
} | |
return data; | |
} | |
-(NSData *)compressWithMaxLength:(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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment