Skip to content

Commit

Permalink
Merge pull request OpenFlutter#475 from 9527001/feat-compress_ios
Browse files Browse the repository at this point in the history
分享小程序,data数据压缩成功,但是image转data 导致图片大小变大
  • Loading branch information
JarvanMo authored Apr 14, 2022
2 parents dc5be0e + 4457611 commit 518f31b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
23 changes: 15 additions & 8 deletions ios/Classes/FluwxShareHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,15 @@ - (void)shareMiniProgram:(FlutterMethodCall *)call result:(FlutterResult)result
NSDictionary *hdImagePath = call.arguments[@"hdImagePath"];
if (hdImagePath != (id) [NSNull null]) {
NSData *imageData = [self getNsDataFromWeChatFile:hdImagePath];
NSString *suffix = hdImagePath[@"suffix"];
BOOL isPNG = [self isPNG:suffix];
BOOL compress = call.arguments[fluwxKeyCompressThumbnail];

UIImage *uiImage = [self getThumbnailFromNSData:imageData size:120 * 1024 isPNG:isPNG compress:compress];
if (isPNG) {
hdImageData = UIImagePNGRepresentation(uiImage);
} else {
hdImageData = UIImageJPEGRepresentation(uiImage, 1);
}
hdImageData = [self getThumbnailDataFromNSData:imageData size:120 * 1024 compress:compress];
// UIImage *uiImage = [self getThumbnailFromNSData:imageData size:120 * 1024 isPNG:isPNG compress:compress];
// if (isPNG) {
// hdImageData = UIImagePNGRepresentation(uiImage);
// } else {
// hdImageData = UIImageJPEGRepresentation(uiImage, 1);
// }
}

dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -332,6 +331,14 @@ - (UIImage *)getThumbnailFromNSData:(NSData *)data size:(NSUInteger)size isPNG:(
return uiImage;
}

- (NSData *)getThumbnailDataFromNSData:(NSData *)data size:(NSUInteger)size compress:(BOOL)compress {
if(compress) {
return [ThumbnailHelper compressImageData:data toByte:size];
} else{
return data;
}
}

- (NSString *)readFileFromAssets:(NSString *)imagePath {
NSArray *array = [self formatAssets:imagePath];
NSString *key;
Expand Down
8 changes: 7 additions & 1 deletion ios/Classes/ThumbnailHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@

@interface ThumbnailHelper : NSObject
+ (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength isPNG:(BOOL)isPNG;
@end


/// NSData 压缩后转NSData
/// @param imageData 来源data
/// @param maxLength 压缩目标值,压缩结果在maxLength的0.9~1之间
+ (NSData *)compressImageData:(NSData *)imageData toByte:(NSUInteger)maxLength;
@end
49 changes: 48 additions & 1 deletion ios/Classes/ThumbnailHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,53 @@

@implementation ThumbnailHelper

+ (NSData *)compressImageData:(NSData *)imageData toByte:(NSUInteger)maxLength {
// Compress by quality
CGFloat compression = 1;
NSData *data = imageData;
NSLog(@"压缩前 %lu %lu ",(unsigned long)data.length,maxLength);
if (data.length < maxLength) return data;

UIImage *image = [UIImage imageWithData:imageData];
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;
}
}

NSLog(@"压缩第一次 %lu %lu ",(unsigned long)data.length,maxLength);
if (data.length < maxLength) return data;

UIImage *resultImage;

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;
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(@"压缩第二次%lu %lu ",(unsigned long)data.length,maxLength);
return data;
}

+ (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength isPNG:(BOOL)isPNG {
// Compress by quality
CGFloat compression = 1;
Expand Down Expand Up @@ -83,4 +130,4 @@ - (UIImage *)scaleFromImage:(UIImage *)image width:(CGSize)newSize {
UIGraphicsEndImageContext();
return newImage;
}
@end
@end

0 comments on commit 518f31b

Please sign in to comment.