使用AFNetworking将图像上传到服务器已损坏 [英] Uploading image to server reached damaged using AFNetworking

查看:112
本文介绍了使用AFNetworking将图像上传到服务器已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力将图像上传到服务器并且上传成功,但在服务器端如果我尝试打开图像,则表示无法显示,因为它包含错误。我在使用AFNetworking将图像转换为JPEGRepresentation后上传图像。

Ive been trying so hard to upload an Image to the server and it uploads successfully but on the server side if i tried opening the image it says cannot be displayed because it contains errors.I am using AFNetworking to upload the image after converting it to JPEGRepresentation.

let uploadManager : AFHTTPSessionManager = AFHTTPSessionManager()

        uploadManager.responseSerializer = AFHTTPResponseSerializer() as AFHTTPResponseSerializer
        uploadManager.requestSerializer = AFHTTPRequestSerializer() as AFHTTPRequestSerializer

        uploadManager.requestSerializer.timeoutInterval = 20

        uploadManager.POST(baseURL, parameters: nil, constructingBodyWithBlock: { (formData) -> Void in

            let selectedImage = self.userImage.image
            let imageData : NSData = UIImageJPEGRepresentation(selectedImage!, 0.5)!
            formData.appendPartWithFileData(imageData, name: "1", fileName: "userimage\(self.objManager.userID)", mimeType: "image/jpeg")

            print("Size of Image(Kbytes):\(imageData.length/1024)")
            print(baseURL)
            }, progress: nil, success: { (task, responseObject) -> Void in

                let jsonData: NSData = responseObject as! NSData
                let dataString = String(data: jsonData, encoding: NSUTF8StringEncoding)
                print(dataString)

            }) { (task, error) -> Void in


        }

图片来自 imagePickerController

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    print("Did finish picking the image succesfully")
    self.userImage.image = image
    picker.dismissViewControllerAnimated(true, completion: nil)

    // call uploading image function


}

导致图像损坏的原因是什么?我一直与后端开发人员联系,他的机制是接收字节数组!将NSData发送到服务器,然后服务器将我的NSData转换为字节数组是否会破坏图像?还是好的?我该如何解决这个问题?我发送图像时有什么问题吗?

What is causing the image to be damaged ? I've been in contact with backend developer and his mechanism is to receive byte array ! sending NSData to the server and then the server convert my NSData to byte array is damaging the image ? or its ok ? How do i fix this issue ? is there is anything wrong I'm doing when i am sending the image ?

推荐答案

1)(任务,错误) - > Void in 打印错误(如果存在),因为如果出现问题,您会收到错误。

1) In (task, error) -> Void in print error if it exists, because if something goes wrong you receive error there.

NSLog("%@", error.localizedDescription)

2)您确定名称:1是否写入?因为这是后端获取文件数据的字段名称(询问后端开发人员的此字段名称)

2) Are you sure that name: "1" is write? Because this is the name of field where the backend get the file data (ask about this field name your backend developer)

3)将文件类型 jpeg 附加到上传图片名称:

3) Append file type jpeg to uploading image name:

formData.appendPartWithFileData(imageData, name: "1", fileName: "userimage\(self.objManager.userID).jpeg", mimeType: "image/jpeg")

这篇关于使用AFNetworking将图像上传到服务器已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆