I'm using Alamofire
to upload files. It's okay to handle jpeg and png images with UIImage[JPEG|PNG]Representation(), but how to convert animated gif files to NSData?
I tried AnimatedGIFImageSerialization
but it's so old and does not work.
How to convert animated Gif UIImage to NSData for Alamofire?
func uploadFile() {
if let fileURL = NSBundle.mainBundle().URLForResource("AarioAi", withExtension: "jpeg"){
var imageData : NSData? = nil
if let image = UIImage(named: "loading2.gif") {
let filetype = "gif"
switch filetype {
case "jpeg", "jpg":
imageData = UIImageJPEGRepresentation(image, 1.0)
case "gif":
imageData = UIImagePNGRepresentation(image)
case "png":
imageData = UIImagePNGRepresentation(image)
default:
imageData = UIImagePNGRepresentation(image)
}
}
Alamofire.upload(.POST, Conf.URL.uploadFile, multipartFormData: {
// POST file[]=xxxx&&file[]=xxxxx
multipartFormData in
multipartFormData.appendBodyPart(fileURL: fileURL, name: "file[]")
multipartFormData.appendBodyPart(data: imageData!, name: "file[]", fileName: "loading2.gif", mimeType: "image/gif")
},
encodingCompletion: {
encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
})
}
}