我的应用目前正在向我的服务器发送大量文件(我说~14Mo),但最终花费的时间比我想要的时间长。
我知道Alamofire会处理下载请求的压缩,但希望在上传时尝试实现此功能。
self.manager.upload(multipartFormData: { multipartFormData in
let photo1Data = UIImagePNGRepresentation(self.client.gif!.frameImages![0])
let photo2Data = UIImagePNGRepresentation(self.client.gif!.frameImages![1])
let photo3Data = UIImagePNGRepresentation(self.client.gif!.frameImages![2])
let photo4Data = UIImagePNGRepresentation(self.client.gif!.frameImages![3])
let photo5Data = UIImagePNGRepresentation(self.client.gif!.frameImages![4])
multipartFormData.append(photo1Data!, withName: "photo1", fileName: "photo1.png", mimeType: "image/png")
multipartFormData.append(photo2Data!, withName: "photo2", fileName: "photo2.png", mimeType: "image/png")
multipartFormData.append(photo3Data!, withName: "photo3", fileName: "photo3.png", mimeType: "image/png")
multipartFormData.append(photo4Data!, withName: "photo4", fileName: "photo4.png", mimeType: "image/png")
multipartFormData.append(photo5Data!, withName: "photo5", fileName: "photo5.png", mimeType: "image/png")
multipartFormData.append(URL(fileURLWithPath: self.client.videoPath!), withName: "video", fileName: "video.mp4", mimeType: "video/mp4")
if (self.client.firstname != nil) {
multipartFormData.append(self.client.firstname!.data(using: String.Encoding.utf8)!, withName: "firstname")
}
if (self.client.lastname != nil) {
multipartFormData.append(self.client.lastname!.data(using: String.Encoding.utf8)!, withName: "lastname")
}
if (self.client.age != nil) {
multipartFormData.append(self.client.age!.data(using: String.Encoding.utf8)!, withName: "age")
}
if (self.client.email != nil) {
multipartFormData.append(self.client.email!.data(using: String.Encoding.utf8)!, withName: "email")
}
if (self.client.male){
multipartFormData.append("true".data(using: String.Encoding.utf8)!, withName: "male")
} else {
multipartFormData.append("false".data(using: String.Encoding.utf8)!, withName: "male")
}
if (self.client.newsletter){
multipartFormData.append("true".data(using: String.Encoding.utf8)!, withName: "newsletter")
} else {
multipartFormData.append("false".data(using: String.Encoding.utf8)!, withName: "newsletter")
}
}, to: url, method: .post, headers: nil, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
let _ = upload.responseJSON(completionHandler: { (response) in
if (response.response?.statusCode == 200) {
do {
try FileManager.default.removeItem(at: URL(fileURLWithPath: self.client.videoPath!))
} catch {
print("DID NOT DELETE VIDEO FILE")
}
self.client.delete()
}
})
break
case .failure(let encodingError):
print("error:\(encodingError)")
break
}
})
其中client和gif是NSManagedObject继承并相互关联的。 gif帧存储在gif.frameImages属性中,生成的videoFile的filePath位于client.videoPath属性中。
在这种情况下有没有办法压缩multipartFormData?
我不想使用UIImageJPEGRepresentation,因为我希望服务器上的文件是真正的png。