我想通过Pod Alamofire(SessionManager)将照片从图库(iOS)上传到服务器(通过代码中的.getWalluploadUrl获取的uploadUrl是正确的) 我正在尝试使用imagePicker选择一张照片。但是接下来在Alamofire上传中会出错:
multipartEncodingFailed(原因:Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(atURL:file:///var/mobile/Containers/Data/Application/366A5FD0-D597-44DC-A6C7-943DDEAB03B7/Documents/asset.JPG,错误:错误域= NSCocoaErrorDomain代码= 260“无法打开文件“ asset.JPG”,因为没有这样的文件。” UserInfo = {NSURL = file:/// var / mobile / Containers / Data / Application / 366A5FD0- D597-44DC-A6C7-943DDEAB03B7 / Documents / asset.JPG,NSFilePath = / var / mobile / Containers / Data / Application / 366A5FD0-D597-44DC-A6C7-943DDEAB03B7 / Documents / asset.JPG,NSUnderlyingError = 0x28375c9f0 {Error Domain = NSPOSIXErrorDomain代码= 2“没有这样的文件或目录”}}))
为什么此文件不存在?用imagePicker在哪里得到错误的本地路径?
代码(我知道这段代码很糟糕,但是我只是想找出本地路径的问题):
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let imageUrl = info[UIImagePickerController.InfoKey.referenceURL] as! NSURL
let imageName = imageUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageName!)
let image = info[UIImagePickerController.InfoKey.originalImage]as! UIImage
let data = image.pngData()
self.getWalluploadUrl { (uploadUrl) in
let sessionManager = SessionManager.default
sessionManager.upload(
multipartFormData: {
data in
data.append(
localPath!, //????? Incorrect path
withName: "file",
fileName: imageName!,
mimeType: "image/jpeg"
)
},
to: uploadUrl,
encodingCompletion: {
(encodingResult) in
switch encodingResult {
case .success(let upload, _, _):
upload
.uploadProgress { (progress) in
print("progress:",progress)
}
.responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
print(json)
case .failure(let error):
print(error)
}
}
case .failure(let error):
print(error)
}
}
)
}
答案 0 :(得分:-2)
我找到了Swift 4.2的正确方法。它的工作原理:
#include <iostream>
#include <string>
using namespace std;
template <typename k>
class stack;
template <typename k>
class node{
private:
friend class stack<k>;
k data;
node<k> *next;
public:
node(k _x): data(_x), next(NULL) {}
};
template <typename k>
class stack{
private:
node<k> *start;
unsigned int i;
public:
stack(): start(NULL), i(0) {}
~stack(){
while(i!=0) pop();
}
void push(k element){
node<k> *ptr;
node<k> *temp;
ptr=new node<k>(element);
if(start==NULL){
start=ptr;
ptr->next==NULL;
}
else{
while(temp->next!=NULL) temp=temp->next;
temp->next=ptr;
ptr->next=NULL;
}
i++;
}
int pop(){
if(i==1){
int item=start->data;
start=NULL;
i=0;
return item;
}
else{
node<k> *temp=start; //k is my typenam in templates
node<k> *top=start;
while(temp->next!=NULL) temp=temp->next; //getting to last element
while(top->next!=temp) top=top->next; //getting to element before the last
top->next=NULL; //setting next to NULL
int item=temp->data; //getting data from element popped
delete(temp); //deleting last node
i--; //decreasing the size
return item; //returning popped element
}
}
bool isempty(){
if(i==0) return 1;
else return 0;
}
int rozmiar(){
return i;
}
};
int main()
{
stack<char> s;
string slowo;
cin>>slowo;
for(int i=0; i<slowo.length(); i++){
s.push(slowo[i]);
}
for(int i=0; i<slowo.length(); i++){
s.pop();
}
return 0;
}
它不需要使用 .path.lastPathComponent。。这已经是完整且正确的本地路径(作为URL)。
注意:所有照片的URL(以及其他文件的URL)都是临时的,下次您启动该应用程序时将有所不同。