我正在尝试将图像保存到本地文件系统,然后稍后使用alamofire检索图像以进行上传。但是,我不断收到以下错误消息。也许我在保存图像文件时犯了一些错误。我一次上传两个文件(一个jpg和一个pdf)。我可以上传的pdf文件很好,只是我上传失败的图片文件。
// Create a URL to save the IMAGE
func createPhotoURL() -> URL {
let fileName = "tempImage_wb.jpg"
let documentsDirectories = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = documentsDirectories.first!
let pdfPageURL = documentDirectory.appendingPathComponent("\(fileName)")
return pdfPageURL
}
// SAVE UIIMAGE TO LOCAL FILE SYSTEM
func saveImageToLocalFile(image: UIImage) -> Void {
// CREATE URL - SAVE TO PATH
let imageURL = createPhotoURL() // CORRECT FULL LENGTH URL FOR FILE UPLAOD
print("IMAGE SAVED TO URL: \(imageURL)")
let imageData = UIImageJPEGRepresentation(image, 1.0)
do{
try imageData?.write(to: imageURL)
self.scannedImageURL = imageURL
}catch{
print("error writing img to local dir")
}
}
// CONSTRUCT URL FROM FILE NAME
let imgFileName = "tempImage_wb.jpg"
let pdfFileName = "tempImagePDF.pdf"
var dir: URL!
do {
dir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
} catch{
print("error re-creating url")
}
let imgURL = dir.appendingPathComponent(imgFileName)
let pdfURL = dir.appendingPathComponent(pdfFileName)
var arrayURLToUpload: [URL] = []
arrayURLToUpload.append(imgURL)
arrayURLToUpload.append(pdfURL)
// PARAMETRS TO SEND
let sendParamters = ["user_id": "1", "hashTagArray": jsonArrayHashTags]
Alamofire.upload(
multipartFormData: { multipartFormData in
for(key, value) in sendParamters{
multipartFormData.append((value.data(using: .utf8)!), withName: key)
}
for fileURL in arrayURLToUpload{
print("fileURL: \(fileURL)")
multipartFormData.append(fileURL, withName: "file[]")
}
},
to: UPLOAD_URL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
/**TRACK PROGRESS OF UPLOAD**/
upload.uploadProgress { progress in
print(progress.fractionCompleted)
}
/***/
case .failure(let encodingError):
print(encodingError)
}
}
)
控制台:
IMAGE SAVED TO URL: file:///var/mobile/Containers/Data/Application/1522F3C7-876C-410E-935A-D3EC07BC2275/Documents/tempImage_wb.jpg
fileURL: file:///var/mobile/Containers/Data/Application/1522F3C7-876C-410E-935A-D3EC07BC2275/Documents/tempImage_wb.jpg
multipartEncodingFailed(Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(atURL: file:///var/mobile/Containers/Data/Application/1522F3C7-876C-410E-935A-D3EC07BC2275/Documents/tempImage_wb.jpg, error: Error Domain=NSCocoaErrorDomain Code=260 "The file “tempImage_wb.jpg” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///var/mobile/Containers/Data/Application/1522F3C7-876C-410E-935A-D3EC07BC2275/Documents/tempImage_wb.jpg, NSFilePath=/var/mobile/Containers/Data/Application/1522F3C7-876C-410E-935A-D3EC07BC2275/Documents/tempImage_wb.jpg, NSUnderlyingError=0x1c0453350 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}))