1
我正在使用Alamofire
來上傳文件。用UIImage [JPEG | PNG] Representation()處理jpeg和png圖像是可以的,但是如何將動畫gif文件轉換爲NSData?我試過AnimatedGIFImageSerialization
但它太舊了,不起作用。 如何將動畫的Gif UIImage轉換爲Alamofire的NSData?Swift Library:如何將動畫Gif UIImage轉換爲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)
}
})
}
}
你可以看看這個帖子。 http://stackoverflow.com/questions/16748355/convert-gif-image-to-nsdata – TangZijian