2
我試圖將React本地資產轉換爲Base64圖像。RNImageToBase64本地模塊崩潰的URL
在NativeModules中實現了一個React Native庫,但它在執行getBase64String(url, callback)
時崩潰了本機iOS應用程序。發送到RNImageToBase64
例URL(通過react-native-camera
生成):
/var/mobile/Containers/Data/Application/8BE6DA73-73A6-4E9A-BDF9-431726243667/Documents/F76931EE-E8F6-431F-8FFF-D481DDA8CC6B.jpg
失敗的行:
libsystem_kernel.dylib`__abort_with_payload:
0x180640d6c <+0>: movz x16, #0x209
0x180640d70 <+4>: svc #0x80
-> 0x180640d74 <+8>: b.lo 0x180640d8c ; <+32>
0x180640d78 <+12>: stp x29, x30, [sp, #-16]!
0x180640d7c <+16>: mov x29, sp
0x180640d80 <+20>: bl 0x1806257b4 ; cerror_nocancel
0x180640d84 <+24>: mov sp, x29
0x180640d88 <+28>: ldp x29, x30, [sp], #16
0x180640d8c <+32>: ret
index.ios.js
行:
[library assetForURL:url resultBlock:^(ALAsset *asset) {
在Xcode誤差那叫納提veModules:
NativeModules.RNImageToBase64.getBase64String(uri, (err, base64) => {
});
RNImageToBase64.m
的崩潰本地模塊:
#import "RNImageToBase64.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <UIKit/UIKit.h>
@implementation RNImageToBase64
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(getBase64String:(NSString *)input callback:(RCTResponseSenderBlock)callback)
{
NSURL *url = [[NSURL alloc] initWithString:input];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:url resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef imageRef = [rep fullScreenImage];
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef]);
NSString *base64Encoded = [imageData base64EncodedStringWithOptions:0];
callback(@[[NSNull null], base64Encoded]);
} failureBlock:^(NSError *error) {
NSLog(@"that didn't work %@", error);
callback(@[error]);
}];
}
@end
你救了我很多麻煩,非常感謝! – Zri