我的图库中有.gif图片,我想使用Alamofire上传此gif图片,我能够上传.png,jpg,jpeg图片,但是当我尝试上传gif图片时,它作为普通图片上传
请找到我的代码
func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : Any])
{
let imageURL = info[UIImagePickerControllerReferenceURL] as! NSURL
//userDefaultsObj.setValue("", forKey: "imageURL")
let imageName = imageURL.lastPathComponent
let imagenamearray = imageName?.components(separatedBy: ".")
self.Imageextension = (imagenamearray?.last)!.lowercased()
var chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
var resizedImg = self.imageResizeFunction(resizeImage: chosenImage)
let array1 = [UInt8](tmpImage!)
print("Resized Value:\(array1.count/1024)")
if (self.Imageextension == "gif")
{
selectedimgview.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}else
{
selectedimgview.image = resizedImg//chosenImage
}
selectedimgview.contentMode = .scaleToFill
self.dismiss(animated: true, completion: nil)
}
使用Alamofire上传代码
Alamofire.upload(multipartFormData: { (multipartFormData) in
if imagepresent
{
if(imageextension == "jpg")
{
multipartFormData.append(UIImageJPEGRepresentation(imgparameters["fileName"] as! UIImage, 0.5)!, withName: "file", fileName: "swift_file.\(imageextension)", mimeType: "image/\(imageextension)")
}
else if(imageextension == "gif")
{
multipartFormData.append(UIImagePNGRepresentation(imgparameters["fileName"] as! UIImage)!, withName: "file", fileName: "swift_file.\(imageextension)", mimeType: "image/\(imageextension)")
}
else
{
multipartFormData.append(UIImagePNGRepresentation(imgparameters["fileName"] as! UIImage)!, withName: "file", fileName: "swift_file.\(imageextension)", mimeType: "image/\(imageextension)")
}
}
for (key, value) in postparams {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to:requestURL,method : methodType,headers : Header)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})
upload.responseJSON { response in
self.handleResponse(response: response, completionBlockX: completionBlock)
print(response.request as Any) // original URL request
}
case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}
}
上面给出的是从图库中选择图像并将其上传到后端的代码,该代码对于.png,.jpg,.jpeg非常适用,当上传.gif图像时,该代码不适用于.gif。正常图像。
感谢您的快速回复。
答案 0 :(得分:1)
回到你说的时候
let imageURL = info[UIImagePickerControllerReferenceURL] as! NSURL
您在磁盘上拥有原始的gif数据。那就是你要上传的。