作者 周泳恩

设置限制图片大小

... ... @@ -12,6 +12,7 @@
#import <React/RCTBridge.h>
#import <React/RCTLog.h>
#import <React/RCTImageLoader.h>
#import "UIImage+RCTCompress.h"
// Define error messages
#define NOT_REGISTERED (@"registerApp required.")
... ... @@ -318,6 +319,9 @@ RCT_EXPORT_METHOD(pay:(NSDictionary *)data
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:url];
[_bridge.imageLoader loadImageWithURLRequest:imageRequest size:CGSizeMake(100, 100) scale:1 clipped:FALSE resizeMode:RCTResizeModeStretch progressBlock:nil partialLoadBlock:nil
completionBlock:^(NSError *error, UIImage *image) {
if ([aData[@"LimitLength"] integerValue]!=0) {
[image compressWithLengthLimit:[aData[@"LimitLength"]integerValue]*1024.f];
}
[self shareToWeixinWithData:aData thumbImage:image scene:aScene callBack:aCallBack];
}];
} else {
... ...
... ... @@ -10,6 +10,7 @@
10D6D7A71F160C250066F0F3 /* RCTWeChatTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 10D6D7A61F160C250066F0F3 /* RCTWeChatTests.m */; };
10D6D7A91F160C250066F0F3 /* libRCTWeChat.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 86D238421BD0BB9E00C75D01 /* libRCTWeChat.a */; };
10D6D7AF1F160C3A0066F0F3 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 10D6D76D1F160B090066F0F3 /* libReact.a */; };
84956EED25E899A700C55280 /* UIImage+RCTCompress.m in Sources */ = {isa = PBXBuildFile; fileRef = 84956EEB25E899A700C55280 /* UIImage+RCTCompress.m */; };
86D238561BD0BC1000C75D01 /* RCTWeChat.m in Sources */ = {isa = PBXBuildFile; fileRef = 86D238551BD0BC1000C75D01 /* RCTWeChat.m */; };
86D2385D1BD0CA3D00C75D01 /* libWeChatSDK.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 86D238571BD0BC2200C75D01 /* libWeChatSDK.a */; settings = {ATTRIBUTES = (Required, ); }; };
/* End PBXBuildFile section */
... ... @@ -125,6 +126,8 @@
10D6D7A41F160C250066F0F3 /* RCTWeChatTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RCTWeChatTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
10D6D7A61F160C250066F0F3 /* RCTWeChatTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCTWeChatTests.m; sourceTree = "<group>"; };
10D6D7A81F160C250066F0F3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
84956EEB25E899A700C55280 /* UIImage+RCTCompress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+RCTCompress.m"; sourceTree = "<group>"; };
84956EEC25E899A700C55280 /* UIImage+RCTCompress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+RCTCompress.h"; sourceTree = "<group>"; };
86D238421BD0BB9E00C75D01 /* libRCTWeChat.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTWeChat.a; sourceTree = BUILT_PRODUCTS_DIR; };
86D238541BD0BC1000C75D01 /* RCTWeChat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTWeChat.h; sourceTree = SOURCE_ROOT; };
86D238551BD0BC1000C75D01 /* RCTWeChat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTWeChat.m; sourceTree = SOURCE_ROOT; };
... ... @@ -185,6 +188,8 @@
86D238391BD0BB9E00C75D01 = {
isa = PBXGroup;
children = (
84956EEC25E899A700C55280 /* UIImage+RCTCompress.h */,
84956EEB25E899A700C55280 /* UIImage+RCTCompress.m */,
86D238571BD0BC2200C75D01 /* libWeChatSDK.a */,
86D238581BD0BC2200C75D01 /* WXApi.h */,
86D238591BD0BC2200C75D01 /* WXApiObject.h */,
... ... @@ -276,6 +281,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = 86D238391BD0BB9E00C75D01;
... ... @@ -406,6 +412,7 @@
buildActionMask = 2147483647;
files = (
86D238561BD0BC1000C75D01 /* RCTWeChat.m in Sources */,
84956EED25E899A700C55280 /* UIImage+RCTCompress.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
... ...
//
// UIImage+RCTCompress.h
// RCTWeChat
//
// Created by admin on 2021/2/26.
// Copyright © 2021 WeFlex. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (RCTCompress)
/*
* 压缩图片方法(先压缩质量再压缩尺寸)
*/
-(NSData *)compressWithLengthLimit:(NSUInteger)maxLength;
/*
* 压缩图片方法(压缩质量)
*/
-(NSData *)compressQualityWithLengthLimit:(NSInteger)maxLength;
/*
* 压缩图片方法(压缩质量二分法)
*/
-(NSData *)compressMidQualityWithLengthLimit:(NSInteger)maxLength;
/*
* 压缩图片方法(压缩尺寸)
*/
-(NSData *)compressBySizeWithLengthLimit:(NSUInteger)maxLength;
@end
... ...
//
// UIImage+RCTCompress.m
// RCTWeChat
//
// Created by admin on 2021/2/26.
// Copyright © 2021 WeFlex. All rights reserved.
//
#import "UIImage+RCTCompress.h"
@implementation UIImage (RCTCompress)
-(NSData *)compressWithLengthLimit:(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;
}
- (NSData *)compressQualityWithLengthLimit:(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 *)compressMidQualityWithLengthLimit:(NSInteger)maxLength{
CGFloat compression = 1;
NSData *data = UIImageJPEGRepresentation(self, compression);
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);
if (data.length < maxLength * 0.9) {
min = compression;
} else if (data.length > maxLength) {
max = compression;
} else {
break;
}
}
return data;
}
-(NSData *)compressBySizeWithLengthLimit:(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;
}
@end
... ...