diff --git a/ios/Classes/FluwxShareHandler.m b/ios/Classes/FluwxShareHandler.m index 692e5922..d0bd3d1a 100644 --- a/ios/Classes/FluwxShareHandler.m +++ b/ios/Classes/FluwxShareHandler.m @@ -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(), ^{ @@ -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; diff --git a/ios/Classes/ThumbnailHelper.h b/ios/Classes/ThumbnailHelper.h index 6924de45..23b20cb6 100644 --- a/ios/Classes/ThumbnailHelper.h +++ b/ios/Classes/ThumbnailHelper.h @@ -7,4 +7,10 @@ @interface ThumbnailHelper : NSObject + (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength isPNG:(BOOL)isPNG; -@end \ No newline at end of file + + +/// NSData 压缩后转NSData +/// @param imageData 来源data +/// @param maxLength 压缩目标值,压缩结果在maxLength的0.9~1之间 ++ (NSData *)compressImageData:(NSData *)imageData toByte:(NSUInteger)maxLength; +@end diff --git a/ios/Classes/ThumbnailHelper.m b/ios/Classes/ThumbnailHelper.m index 12121d06..02e012ee 100644 --- a/ios/Classes/ThumbnailHelper.m +++ b/ios/Classes/ThumbnailHelper.m @@ -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; @@ -83,4 +130,4 @@ - (UIImage *)scaleFromImage:(UIImage *)image width:(CGSize)newSize { UIGraphicsEndImageContext(); return newImage; } -@end \ No newline at end of file +@end