gpt4 book ai didi

ios - 在 React Native 中将图像附加到电子邮件(使用 rn-image-picker 和 rn-mail)

转载 作者:可可西里 更新时间:2023-11-01 04:43:05 28 4
gpt4 key购买 nike

11/04 更新:

我已经用一个应用程序创建了一个代码库,它在下面演示了这个问题。任何想要安装和使用它的人都可以在这里找到它...... https://github.com/geirman/react-native-photo-emailer


我正在同时使用以下软件包,试图从图库中选择一张图片(或使用相机拍摄一张图片),然后将该照片附加到电子邮件中。

以下代码在 Android 上运行良好,但无法在 iOS 上附加图像。

import ImagePicker from 'react-native-image-picker';
handleAndroid = (subject, to) => {
var pickerOptions = {
title: 'Select Avatar',
storageOptions: {
skipBackup: true,
path: 'images'
}
};

ImagePicker.showImagePicker(pickerOptions, (response) => {
const { uri, type, path } = response;
this.sendMail(subject, to, path, type);
});
}

sendMail 方法看起来像这样(减去错误处理以简化)

import Mailer from 'react-native-mail';
sendMail = (subject, to, uri, type) => {
Mailer.mail({
subject,
recipients: [to],
body: 'Optional Comment: ',
attachment: {
path: uri,
type: type,
name: subject
},
isHTML: true
}
}

请注意,attachment.path 必须是附件的完整文件路径。对于 Android,结果是 response.path,但 iOS 只返回 response.uri

对于 iOS 的 sendMail 方法,我删除了 attachment.nameattachment.type 因为它们没有返回,只留下 attachment.path设置为 response.uri

response.path(在 Android 上)看起来像这样(有效)...

/storage/emulated/0/DCIM/Camera/IMG_20171102_11304344.jpg 

response.uri(在 iOS 上)看起来像这样(response.path 在 iOS 上不返回任何内容)...

file://var/mobile/Containers/Data/Application/983938D-5304-463C-BD05-D033E55F5BEB/Documents/images/224CA6DD-5299-48C3-A7CF-0B645004535F.jpg

但根据React native - send photo per mail ,我想要看起来更像这样的东西(在 iOS 上)...

/Users/anton/Library/Developer/CoreSimulator/Devices/9A15F203-9A58-41C5-A4FC-EA25FAAE92BD/data/Containers/Data/Application/79FF93F9-BA89-4F4C-8809-277BEECD447D/Documents/EFFF0ECE-4063-4FE5-984E-E76506788350.jpg

所以我的问题是,如何从上面的 response.uri 值获取 iOS 上图像的完整路径。换句话说,如何从 ImagePicker 返回的内容(例如 file://var/mobile. ..)?

更新:

我忘了说我正在实际设备上运行这些测试。 Android Moto Z 和 iPhone 5s。这是必要的,因为模拟器在测试相机和电子邮件功能时毫无用处。

这是与一个 session 相关的我的 xcode 日志的转储:初始化应用 > 从图库中选择照片 > 电子邮件打开(无附件)> 取消 > 删除草稿(抛出“电子邮件发送失败”错误,即使我发送了它虽然电子邮件确实发送了,但我收到了这个错误)

2017-11-02 20:12:45.310 [info][tid:main][RCTCxxBridge.mm:187] Initializing <RCTCxxBridge: 0x1c01b5fc0> (parent: <RCTBridge: 0x1c00babe0>, executor: (null))
2017-11-02 20:12:45.314031-0700 RNMail[434:92814] Initializing <RCTCxxBridge: 0x1c01b5fc0> (parent: <RCTBridge: 0x1c00babe0>, executor: (null))
2017-11-02 20:12:45.421 [warn][tid:main][RCTBridge.m:121] Class RCTCxxModule was not exported. Did you forget to use RCT_EXPORT_MODULE()?
2017-11-02 20:12:45.421568-0700 RNMail[434:92814] Class RCTCxxModule was not exported. Did you forget to use RCT_EXPORT_MODULE()?
2017-11-02 20:12:45.492 [warn][tid:main][RCTModuleData.mm:69] Module RNMail requires main queue setup since it overrides `init` but doesn't implement `requiresMainQueueSetup. In a future release React Native will default to initializing all native modules on a background thread unless explicitly opted-out of.
2017-11-02 20:12:45.492054-0700 RNMail[434:92814] Module RNMail requires main queue setup since it overrides `init` but doesn't implement `requiresMainQueueSetup. In a future release React Native will default to initializing all native modules on a background thread unless explicitly opted-out of.
2017-11-02 20:12:45.504 [info][tid:main][RCTRootView.m:301] Running application RNMail ({
initialProps = {
};
rootTag = 1;
})
2017-11-02 20:12:45.503900-0700 RNMail[434:92814] Running application RNMail ({
initialProps = {
};
rootTag = 1;
})
2017-11-02 20:12:45.537105-0700 RNMail[434:92814] refreshPreferences: HangTracerEnabled: 0
2017-11-02 20:12:45.537237-0700 RNMail[434:92814] refreshPreferences: HangTracerDuration: 500
2017-11-02 20:12:45.537355-0700 RNMail[434:92814] refreshPreferences: ActivationLoggingEnabled: 0 ActivationLoggingTaskedOffByDA:0
=================================================================
Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 434, TID: 92857, Thread name: com.facebook.react.JavaScript, Queue name: com.apple.root.default-qos.overcommit, QoS: 21
Backtrace:
4 RNMail 0x000000010026d2bc RCTCurrentAppBackgroundState + 220
5 RNMail 0x000000010026c7f8 -[RCTAppState constantsToExport] + 68
6 RNMail 0x0000000100275af4 -[RCTModuleData gatherConstants] + 724
7 RNMail 0x0000000100275ea8 -[RCTModuleData exportedConstants] + 36
8 RNMail 0x00000001002d312c _ZN8facebook5react15RCTNativeModule12getConstantsEv + 216
9 RNMail 0x000000010035764c _ZN8facebook5react14ModuleRegistry9getConfigERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE + 5864
10 RNMail 0x000000010038d8e0 _ZN8facebook5react16JSCNativeModules12createModuleERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEPK15OpaqueJSContext + 492
11 RNMail 0x000000010038c5a0 _ZN8facebook5react16JSCNativeModules9getModuleEPK15OpaqueJSContextP14OpaqueJSString + 640
12 RNMail 0x00000001003708c0 _ZN8facebook5react11JSCExecutor15getNativeModuleEP13OpaqueJSValueP14OpaqueJSString + 268
13 RNMail 0x000000010037313c _ZZN8facebook5react12_GLOBAL__N_119exceptionWrapMethodIXadL_ZNS0_11JSCExecutor15getNativeModuleEP13OpaqueJSValueP14OpaqueJSStringEEEEPFPKS4_PK15OpaqueJSContextS5_S7_PS9_EvEN11funcWrapper4callESC_S5_S7_SD_ + 356
14 JavaScriptCore 0x000000018df6cdc4 <redacted> + 336
15 JavaScriptCore 0x000000018d950394 <redacted> + 3952
16 JavaScriptCore 0x000000018e052a94 <redacted> + 10612
17 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
18 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
19 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
20 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
21 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
22 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
23 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
24 JavaScriptCore 0x000000018e04ff50 <redacted> + 272
25 JavaScriptCore 0x000000018df35b94 <redacted> + 136
26 JavaScriptCore 0x000000018df0cb28 <redacted> + 11244
27 JavaScriptCore 0x000000018dc011ac _ZN3JSC8evaluateEPNS_9ExecStateERKNS_10SourceCodeENS_7JSValueERN3WTF8NakedPtrINS_9ExceptionEEE + 316
28 JavaScriptCore 0x000000018df6a558 JSEvaluateScript + 448
29 RNMail 0x00000001002fcc58 _ZN8facebook5react14evaluateScriptEPK15OpaqueJSContextP14OpaqueJSStringS5_ + 100
30 RNMail 0x000000010036d494 _ZN8facebook5react11JSCExecutor21loadApplicationScriptENSt3__110unique_ptrIKNS0_11JSBigStringENS2_14default_deleteIS5_EEEENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE + 2528
31 RNMail 0x0000000100384d14 _ZZN8facebook5react16NativeToJsBridge15loadApplicationENSt3__110unique_ptrINS0_17JSModulesUnbundleENS2_14default_deleteIS4_EEEENS3_IKNS0_11JSBigStringENS5_IS9_EEEENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEN3$_0clEPNS0_10JSExecutorE + 1372
32 RNMail 0x00000001003847ac _ZNSt3__128__invoke_void_return_wrapperIvE6__callIJRZN8facebook5react16NativeToJsBridge15loadApplicationENS_10unique_ptrINS4_17JSModulesUnbundleENS_14default_deleteIS7_EEEENS6_IKNS4_11JSBigStringENS8_ISC_EEEENS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEEE3$_0PNS4_10JSExecutorEEEEvDpOT_ + 84
33 RNMail 0x0000000100383da0 _ZNSt3__110__function6__funcIZN8fa2017-11-02 20:12:46.253004-0700 RNMail[434:92857] [reports] Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 434, TID: 92857, Thread name: com.facebook.react.JavaScript, Queue name: com.apple.root.default-qos.overcommit, QoS: 21
Backtrace:
4 RNMail 0x000000010026d2bc RCTCurrentAppBackgroundState + 220
5 RNMail 0x000000010026c7f8 -[RCTAppState constantsToExport] + 68
6 RNMail 0x0000000100275af4 -[RCTModuleData gatherConstants] + 724
7 RNMail 0x0000000100275ea8 -[RCTModuleData exportedConstants] + 36
8 RNMail 0x00000001002d312c _ZN8facebook5react15RCTNativeModule12getConstantsEv + 216
9 RNMail 0x000000010035764c _ZN8facebook5react14ModuleRegistry9getConfigERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE + 5864
10 RNMail 0x000000010038d8e0 _ZN8facebook5react16JSCNativeModules12createModuleERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEPK15OpaqueJSContext + 492
11 RNMail 0x000000010038c5a0 _ZN8facebook5react16JSCNativeModules9getModuleEPK15OpaqueJSContextP14OpaqueJSString + 640
12 RNMail 0x00000001003708c0 _ZN8facebook5react11JSCExecutor15getNativeModuleEP13OpaqueJSValueP14OpaqueJSString + 268
13 RNMail 0x000000010037313c _ZZN8facebook5react12_GLOBAL__N_119exceptionWrapMethodIXadL_ZNS0_11JSCExecutor15getNativeModuleEP13OpaqueJSValueP14OpaqueJSStringEEEEPFPKS4_PK15OpaqueJSContextS5_S7_PS9_EvEN11funcWrapper4callESC_S5_S7_SD_ + 356
14 JavaScriptCore 0x000000018df6cdc4 <redacted> + 336
15 JavaScriptCore 0x000000018d950394 <redacted> + 3952
16 JavaScriptCore 0x000000018e052a94 <redacted> + 10612
17 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
18 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
19 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
20 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
21 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
22 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
23 JavaScriptCore 0x000000018e056a94 <redacted> + 26996
24 JavaScriptCore 0x000000018e04ff50 <redacted> + 272
25 JavaScriptCore 0x000000018df35b94 <redacted> + 136
26 JavaScriptCore 0x000000018df0cb28 <redacted> + 11244
27 JavaScriptCore 0x000000018dc011ac _ZN3JSC8evaluateEPNS_9ExecStateERKNS_10SourceCodeENS_7JSValueERN3WTF8NakedPtrINS_9ExceptionEEE + 316
28 JavaScriptCore 0x000000018df6a558 JSEvaluateScript + 448
29 RNMail 0x00000001002fcc58 _ZN8facebook5react14evaluateScriptEPK15OpaqueJSContextP14OpaqueJSStringS5_ + 100
30 RNMail 0x000000010036d494 _ZN8facebook5react11JSCExecutor21loadApplicationScriptENSt3__110unique_ptrIKNS0_11JSBigStringENS2_14default_deleteIS5_EEEENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE + 2528
31 RNMail 0x0000000100384d14 _ZZN8facebook5react16NativeToJsBridge15loadApplicationENSt3__110unique_ptrINS0_17JSModulesUnbundleENS2_14default_deleteIS4_EEEENS3_IKNS0_11JSBigStringENS5_IS9_EEEENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEN3$_0clEPNS0_10JSExecutorE + 1372
32 RNMail 0x00000001003847ac _ZNSt3__128__invoke_void_return_wrapperIvE6__callIJRZN8facebook5react16NativeToJsBridge15loadApplicationENS_10unique_ptrINS4_17JSModulesUnbundleENS_14default_deleteIS7_EEEENS6_IKNS4_11JSBigStringENS8_ISC_EEEENS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEEE3$_0PNS4_10JSExecutorEEEEvDpOT_ + 84
33 RNMail 0x0000000100383da0 _ZNSt3__110__function6__funcIZN8fa
2017-11-02 20:12:46.621 [info][tid:com.facebook.react.JavaScript] Running application "RNMail" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
2017-11-02 20:12:46.621391-0700 RNMail[434:92857] Running application "RNMail" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
2017-11-02 20:12:47.742027-0700 RNMail[434:92844] [] nw_connection_get_connected_socket 4 Connection has no connected handler
2017-11-02 20:12:47.742187-0700 RNMail[434:92844] TCP Conn 0x1c4170200 Failed : error 0:61 [61]
2017-11-02 20:12:50.900283-0700 RNMail[434:92848] [] nw_connection_get_connected_socket 5 Connection has no connected handler
2017-11-02 20:12:50.900432-0700 RNMail[434:92848] TCP Conn 0x1c0171400 Failed : error 0:61 [61]
2017-11-02 20:12:53.627866-0700 RNMail[434:92814] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-11-02 20:12:53.628903-0700 RNMail[434:92814] [MC] Reading from public effective user settings.
2017-11-02 20:12:54.060535-0700 RNMail[434:92843] [] nw_connection_get_connected_socket 6 Connection has no connected handler
2017-11-02 20:12:54.060707-0700 RNMail[434:92843] TCP Conn 0x1c416e400 Failed : error 0:61 [61]
2017-11-02 20:12:57.279004-0700 RNMail[434:92847] [] nw_connection_get_connected_socket 7 Connection has no connected handler
2017-11-02 20:12:57.279148-0700 RNMail[434:92847] TCP Conn 0x1c0172b40 Failed : error 0:61 [61]
2017-11-02 20:13:00.409536-0700 RNMail[434:92848] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
2017-11-02 20:13:00.429314-0700 RNMail[434:93038] [] nw_connection_get_connected_socket 8 Connection has no connected handler
2017-11-02 20:13:00.429401-0700 RNMail[434:93038] TCP Conn 0x1c0171c40 Failed : error 0:61 [61]
2017-11-02 20:13:00.827 [info][tid:com.facebook.react.JavaScript] 'Response = ', { fileSize: 508257,
data: REMOVED_BASE64_STRING
timestamp: '2017-10-25T02:51:35Z',
uri: 'file:///var/mobile/Containers/Data/Application/144D1C48-C6D5-475A-891A-F75360CD64E2/Documents/images/0EF09235-6E04-4799-86F5-3E996ED4E439.jpg',
origURL: 'assets-library://asset/asset.JPG?id=EF345E7A-2DF1-49CA-875D-4208976A635B&ext=JPG',
isVertical: false,
height: 768,
width: 1024,
fileName: 'IMG_0003.JPG' }
2017-11-02 20:13:00.888025-0700 RNMail[434:92857] 'Response = ', { fileSize: 508257,
data: REMOVED_BASE64_STRING
2017-11-02 20:13:00.974189-0700 RNMail[434:92814] [MC] Filtering mail sheet accounts for bundle ID: org.reactjs.native.example.RNMail, source account management: 1
2017-11-02 20:13:00.985481-0700 RNMail[434:92814] [MC] Filtering mail sheet accounts for bundle ID: org.reactjs.native.example.RNMail, source account management: 1
2017-11-02 20:13:01.834117-0700 RNMail[434:92814] [MC] Lazy loading NSBundle MobileCoreServices.framework
2017-11-02 20:13:01.843828-0700 RNMail[434:92814] [MC] Loaded MobileCoreServices.framework
2017-11-02 20:13:04.218796-0700 RNMail[434:93034] [] nw_connection_get_connected_socket 9 Connection has no connected handler
2017-11-02 20:13:04.218943-0700 RNMail[434:93034] TCP Conn 0x1c01747c0 Failed : error 0:61 [61]
2017-11-02 20:13:07.341962-0700 RNMail[434:93036] [] nw_connection_get_connected_socket 10 Connection has no connected handler
2017-11-02 20:13:07.342107-0700 RNMail[434:93036] TCP Conn 0x1c0172540 Failed : error 0:61 [61]
2017-11-02 20:13:08.605 [info][tid:com.facebook.react.JavaScript] null
2017-11-02 20:13:08.606476-0700 RNMail[434:92857] null
2017-11-02 20:13:10.469212-0700 RNMail[434:93034] [] nw_connection_get_connected_socket 11 Connection has no connected handler
2017-11-02 20:13:10.469352-0700 RNMail[434:93034] TCP Conn 0x1c4174280 Failed : error 0:61 [61]
2017-11-02 20:13:11.144 [info][tid:com.facebook.react.JavaScript] OK: Email Error Response
2017-11-02 20:13:11.144088-0700 RNMail[434:92857] OK: Email Error Response
2017-11-02 20:13:13.599247-0700 RNMail[434:93036] [] nw_connection_get_connected_socket 12 Connection has no connected handler
2017-11-02 20:13:13.599391-0700 RNMail[434:93036] TCP Conn 0x1c0175480 Failed : error 0:61 [61]

2017 年 11 月 7 日更新

在@Artal 的建议下,我开始深入研究 Xcode 调试器。我发现 attachmentPath 被分配给 fileData 作为 NSData 类型,然后它被用来实际将照片附加到电子邮件。然而,虽然看起来 fileData 被正确分配了,但只要我进一步进入代码一行,就会以某种方式 fileData = nill 。下面是逐步执行代码的一些屏幕截图。

enter image description here

最佳答案

@Artal 让我走上了正确的道路,但这是最终解决问题的代码......

Replace Line 82 of RNMail.m

NSData *fileData = [NSData dataWithContentsOfFile:attachmentPath];

使用这段代码...

// Get the URL string, which is *not* a path (e.g. because it's file:// based)
NSString *attachmentURLString = [RCTConvert NSString:options[@"attachment"][@"path"]];
// Create a URL from the string
NSURL *attachmentURL = [[NSURLComponents componentsWithString:attachmentURLString] URL];

// Get the resource path and read the file using NSData
NSError *error = nil;
NSData *fileData = [NSData dataWithContentsOfURL:attachmentURL options:0 error:&error];

if(fileData == nil) {
// handle error
}

有关如何得出此解决方案的更多信息,请参阅 NSData Assignment Vanishes (becomes nil) Directly After Assigned

关于ios - 在 React Native 中将图像附加到电子邮件(使用 rn-image-picker 和 rn-mail),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47086300/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com