gpt4 book ai didi

ios - 如何从 UIImagepicker 中选取动画图像并将其添加到 UIimageview 并上传

转载 作者:行者123 更新时间:2023-11-28 14:15:32 25 4
gpt4 key购买 nike

我的图库中有 .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 数据。那就是你要上传的。

关于ios - 如何从 UIImagepicker 中选取动画图像并将其添加到 UIimageview 并上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52217456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com