我在我的画廊中有.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图像显示为普通图像。
感谢您的快速回复。
回来的时候你说
let imageURL = info[UIImagePickerControllerReferenceURL] as! NSURL
你在磁盘上有原始的gif数据。这就是你想要上传的内容。