作者 yang

Merge branch 'master' of 47.107.73.162:xiaoming/react-native-wechat

@@ -12,6 +12,7 @@ @@ -12,6 +12,7 @@
12 #import <React/RCTBridge.h> 12 #import <React/RCTBridge.h>
13 #import <React/RCTLog.h> 13 #import <React/RCTLog.h>
14 #import <React/RCTImageLoader.h> 14 #import <React/RCTImageLoader.h>
  15 +#import "UIImage+RCTCompress.h"
15 16
16 // Define error messages 17 // Define error messages
17 #define NOT_REGISTERED (@"registerApp required.") 18 #define NOT_REGISTERED (@"registerApp required.")
@@ -318,6 +319,12 @@ RCT_EXPORT_METHOD(pay:(NSDictionary *)data @@ -318,6 +319,12 @@ RCT_EXPORT_METHOD(pay:(NSDictionary *)data
318 NSURLRequest *imageRequest = [NSURLRequest requestWithURL:url]; 319 NSURLRequest *imageRequest = [NSURLRequest requestWithURL:url];
319 [_bridge.imageLoader loadImageWithURLRequest:imageRequest size:CGSizeMake(100, 100) scale:1 clipped:FALSE resizeMode:RCTResizeModeStretch progressBlock:nil partialLoadBlock:nil 320 [_bridge.imageLoader loadImageWithURLRequest:imageRequest size:CGSizeMake(100, 100) scale:1 clipped:FALSE resizeMode:RCTResizeModeStretch progressBlock:nil partialLoadBlock:nil
320 completionBlock:^(NSError *error, UIImage *image) { 321 completionBlock:^(NSError *error, UIImage *image) {
  322 + if ([aData[@"LimitLength"] integerValue]!=0) {
  323 + [image compressWithLengthLimit:[aData[@"LimitLength"]integerValue]*1024.f];
  324 + }else
  325 + {
  326 + [image compressWithLengthLimit:32*1024.f];
  327 + }
321 [self shareToWeixinWithData:aData thumbImage:image scene:aScene callBack:aCallBack]; 328 [self shareToWeixinWithData:aData thumbImage:image scene:aScene callBack:aCallBack];
322 }]; 329 }];
323 } else { 330 } else {
@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
10 10D6D7A71F160C250066F0F3 /* RCTWeChatTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 10D6D7A61F160C250066F0F3 /* RCTWeChatTests.m */; }; 10 10D6D7A71F160C250066F0F3 /* RCTWeChatTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 10D6D7A61F160C250066F0F3 /* RCTWeChatTests.m */; };
11 10D6D7A91F160C250066F0F3 /* libRCTWeChat.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 86D238421BD0BB9E00C75D01 /* libRCTWeChat.a */; }; 11 10D6D7A91F160C250066F0F3 /* libRCTWeChat.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 86D238421BD0BB9E00C75D01 /* libRCTWeChat.a */; };
12 10D6D7AF1F160C3A0066F0F3 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 10D6D76D1F160B090066F0F3 /* libReact.a */; }; 12 10D6D7AF1F160C3A0066F0F3 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 10D6D76D1F160B090066F0F3 /* libReact.a */; };
  13 + 84956EED25E899A700C55280 /* UIImage+RCTCompress.m in Sources */ = {isa = PBXBuildFile; fileRef = 84956EEB25E899A700C55280 /* UIImage+RCTCompress.m */; };
13 86D238561BD0BC1000C75D01 /* RCTWeChat.m in Sources */ = {isa = PBXBuildFile; fileRef = 86D238551BD0BC1000C75D01 /* RCTWeChat.m */; }; 14 86D238561BD0BC1000C75D01 /* RCTWeChat.m in Sources */ = {isa = PBXBuildFile; fileRef = 86D238551BD0BC1000C75D01 /* RCTWeChat.m */; };
14 86D2385D1BD0CA3D00C75D01 /* libWeChatSDK.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 86D238571BD0BC2200C75D01 /* libWeChatSDK.a */; settings = {ATTRIBUTES = (Required, ); }; }; 15 86D2385D1BD0CA3D00C75D01 /* libWeChatSDK.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 86D238571BD0BC2200C75D01 /* libWeChatSDK.a */; settings = {ATTRIBUTES = (Required, ); }; };
15 /* End PBXBuildFile section */ 16 /* End PBXBuildFile section */
@@ -125,6 +126,8 @@ @@ -125,6 +126,8 @@
125 10D6D7A41F160C250066F0F3 /* RCTWeChatTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RCTWeChatTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 126 10D6D7A41F160C250066F0F3 /* RCTWeChatTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RCTWeChatTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
126 10D6D7A61F160C250066F0F3 /* RCTWeChatTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCTWeChatTests.m; sourceTree = "<group>"; }; 127 10D6D7A61F160C250066F0F3 /* RCTWeChatTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCTWeChatTests.m; sourceTree = "<group>"; };
127 10D6D7A81F160C250066F0F3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; 128 10D6D7A81F160C250066F0F3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
  129 + 84956EEB25E899A700C55280 /* UIImage+RCTCompress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+RCTCompress.m"; sourceTree = "<group>"; };
  130 + 84956EEC25E899A700C55280 /* UIImage+RCTCompress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+RCTCompress.h"; sourceTree = "<group>"; };
128 86D238421BD0BB9E00C75D01 /* libRCTWeChat.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTWeChat.a; sourceTree = BUILT_PRODUCTS_DIR; }; 131 86D238421BD0BB9E00C75D01 /* libRCTWeChat.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTWeChat.a; sourceTree = BUILT_PRODUCTS_DIR; };
129 86D238541BD0BC1000C75D01 /* RCTWeChat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTWeChat.h; sourceTree = SOURCE_ROOT; }; 132 86D238541BD0BC1000C75D01 /* RCTWeChat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTWeChat.h; sourceTree = SOURCE_ROOT; };
130 86D238551BD0BC1000C75D01 /* RCTWeChat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTWeChat.m; sourceTree = SOURCE_ROOT; }; 133 86D238551BD0BC1000C75D01 /* RCTWeChat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTWeChat.m; sourceTree = SOURCE_ROOT; };
@@ -185,6 +188,8 @@ @@ -185,6 +188,8 @@
185 86D238391BD0BB9E00C75D01 = { 188 86D238391BD0BB9E00C75D01 = {
186 isa = PBXGroup; 189 isa = PBXGroup;
187 children = ( 190 children = (
  191 + 84956EEC25E899A700C55280 /* UIImage+RCTCompress.h */,
  192 + 84956EEB25E899A700C55280 /* UIImage+RCTCompress.m */,
188 86D238571BD0BC2200C75D01 /* libWeChatSDK.a */, 193 86D238571BD0BC2200C75D01 /* libWeChatSDK.a */,
189 86D238581BD0BC2200C75D01 /* WXApi.h */, 194 86D238581BD0BC2200C75D01 /* WXApi.h */,
190 86D238591BD0BC2200C75D01 /* WXApiObject.h */, 195 86D238591BD0BC2200C75D01 /* WXApiObject.h */,
@@ -276,6 +281,7 @@ @@ -276,6 +281,7 @@
276 developmentRegion = English; 281 developmentRegion = English;
277 hasScannedForEncodings = 0; 282 hasScannedForEncodings = 0;
278 knownRegions = ( 283 knownRegions = (
  284 + English,
279 en, 285 en,
280 ); 286 );
281 mainGroup = 86D238391BD0BB9E00C75D01; 287 mainGroup = 86D238391BD0BB9E00C75D01;
@@ -406,6 +412,7 @@ @@ -406,6 +412,7 @@
406 buildActionMask = 2147483647; 412 buildActionMask = 2147483647;
407 files = ( 413 files = (
408 86D238561BD0BC1000C75D01 /* RCTWeChat.m in Sources */, 414 86D238561BD0BC1000C75D01 /* RCTWeChat.m in Sources */,
  415 + 84956EED25E899A700C55280 /* UIImage+RCTCompress.m in Sources */,
409 ); 416 );
410 runOnlyForDeploymentPostprocessing = 0; 417 runOnlyForDeploymentPostprocessing = 0;
411 }; 418 };
  1 +//
  2 +// UIImage+RCTCompress.h
  3 +// RCTWeChat
  4 +//
  5 +// Created by admin on 2021/2/26.
  6 +// Copyright © 2021 WeFlex. All rights reserved.
  7 +//
  8 +
  9 +#import <UIKit/UIKit.h>
  10 +
  11 +
  12 +@interface UIImage (RCTCompress)
  13 +/*
  14 + * 压缩图片方法(先压缩质量再压缩尺寸)
  15 + */
  16 +-(NSData *)compressWithLengthLimit:(NSUInteger)maxLength;
  17 +/*
  18 + * 压缩图片方法(压缩质量)
  19 + */
  20 +-(NSData *)compressQualityWithLengthLimit:(NSInteger)maxLength;
  21 +/*
  22 + * 压缩图片方法(压缩质量二分法)
  23 + */
  24 +-(NSData *)compressMidQualityWithLengthLimit:(NSInteger)maxLength;
  25 +/*
  26 + * 压缩图片方法(压缩尺寸)
  27 + */
  28 +-(NSData *)compressBySizeWithLengthLimit:(NSUInteger)maxLength;
  29 +@end
  30 +
  1 +//
  2 +// UIImage+RCTCompress.m
  3 +// RCTWeChat
  4 +//
  5 +// Created by admin on 2021/2/26.
  6 +// Copyright © 2021 WeFlex. All rights reserved.
  7 +//
  8 +
  9 +#import "UIImage+RCTCompress.h"
  10 +
  11 +@implementation UIImage (RCTCompress)
  12 +-(NSData *)compressWithLengthLimit:(NSUInteger)maxLength{
  13 + // Compress by quality
  14 + CGFloat compression = 1;
  15 + NSData *data = UIImageJPEGRepresentation(self, compression);
  16 + //NSLog(@"Before compressing quality, image size = %ld KB",data.length/1024);
  17 + if (data.length < maxLength) return data;
  18 +
  19 + CGFloat max = 1;
  20 + CGFloat min = 0;
  21 + for (int i = 0; i < 6; ++i) {
  22 + compression = (max + min) / 2;
  23 + data = UIImageJPEGRepresentation(self, compression);
  24 + //NSLog(@"Compression = %.1f", compression);
  25 + //NSLog(@"In compressing quality loop, image size = %ld KB", data.length / 1024);
  26 + if (data.length < maxLength * 0.9) {
  27 + min = compression;
  28 + } else if (data.length > maxLength) {
  29 + max = compression;
  30 + } else {
  31 + break;
  32 + }
  33 + }
  34 + //NSLog(@"After compressing quality, image size = %ld KB", data.length / 1024);
  35 + if (data.length < maxLength) return data;
  36 + UIImage *resultImage = [UIImage imageWithData:data];
  37 + // Compress by size
  38 + NSUInteger lastDataLength = 0;
  39 + while (data.length > maxLength && data.length != lastDataLength) {
  40 + lastDataLength = data.length;
  41 + CGFloat ratio = (CGFloat)maxLength / data.length;
  42 + //NSLog(@"Ratio = %.1f", ratio);
  43 + CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
  44 + (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
  45 + UIGraphicsBeginImageContext(size);
  46 + [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
  47 + resultImage = UIGraphicsGetImageFromCurrentImageContext();
  48 + UIGraphicsEndImageContext();
  49 + data = UIImageJPEGRepresentation(resultImage, compression);
  50 + //NSLog(@"In compressing size loop, image size = %ld KB", data.length / 1024);
  51 + }
  52 + //NSLog(@"After compressing size loop, image size = %ld KB", data.length / 1024);
  53 + return data;
  54 +}
  55 +
  56 +- (NSData *)compressQualityWithLengthLimit:(NSInteger)maxLength {
  57 + CGFloat compression = 1;
  58 + NSData *data = UIImageJPEGRepresentation(self, compression);
  59 + while (data.length > maxLength && compression > 0) {
  60 + compression -= 0.02;
  61 + data = UIImageJPEGRepresentation(self, compression); // When compression less than a value, this code dose not work
  62 + }
  63 + return data;
  64 +}
  65 +
  66 +
  67 +-(NSData *)compressMidQualityWithLengthLimit:(NSInteger)maxLength{
  68 + CGFloat compression = 1;
  69 + NSData *data = UIImageJPEGRepresentation(self, compression);
  70 + if (data.length < maxLength) return data;
  71 + CGFloat max = 1;
  72 + CGFloat min = 0;
  73 + for (int i = 0; i < 6; ++i) {
  74 + compression = (max + min) / 2;
  75 + data = UIImageJPEGRepresentation(self, compression);
  76 + if (data.length < maxLength * 0.9) {
  77 + min = compression;
  78 + } else if (data.length > maxLength) {
  79 + max = compression;
  80 + } else {
  81 + break;
  82 + }
  83 + }
  84 + return data;
  85 +}
  86 +
  87 +-(NSData *)compressBySizeWithLengthLimit:(NSUInteger)maxLength{
  88 + UIImage *resultImage = self;
  89 + NSData *data = UIImageJPEGRepresentation(resultImage, 1);
  90 + NSUInteger lastDataLength = 0;
  91 + while (data.length > maxLength && data.length != lastDataLength) {
  92 + lastDataLength = data.length;
  93 + CGFloat ratio = (CGFloat)maxLength / data.length;
  94 + CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
  95 + (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
  96 + UIGraphicsBeginImageContext(size);
  97 + // Use image to draw (drawInRect:), image is larger but more compression time
  98 + // Use result image to draw, image is smaller but less compression time
  99 + [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
  100 + resultImage = UIGraphicsGetImageFromCurrentImageContext();
  101 + UIGraphicsEndImageContext();
  102 + data = UIImageJPEGRepresentation(resultImage, 1);
  103 + }
  104 + return data;
  105 +}
  106 +@end