简体   繁体   English

使用AFNetworking将图像上传到服务器已损坏

[英]Uploading image to server reached damaged using AFNetworking

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. 我一直很努力地将图像上传到服务器,并且成功上传,但是在服务器端,如果我尝试打开图像, cannot be displayed because it contains errors它说cannot be displayed because it contains errors 。我正在使用AFNetworking在将图像转换为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


        }

The image comes from imagePickerController : 该图像来自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


}

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 ? 将NSData发送到服务器,然后服务器将我的NSData转换为字节数组会损坏映像吗? or its ok ? 还是可以吗? How do i fix this issue ? 我该如何解决这个问题? is there is anything wrong I'm doing when i am sending the image ? 发送图像时我在做什么错?

1) In (task, error) -> Void in print error if it exists, because if something goes wrong you receive error there. 1)(task, error) -> Void in如果存在打印错误,则显示(task, error) -> Void in ,因为如果出现问题,您将在此收到错误。

NSLog("%@", error.localizedDescription)

2) Are you sure that name: "1" is write? 2)您确定name: "1"吗? Because this is the name of field where the backend get the file data (ask about this field name your backend developer) 因为这是后端获取文件数据的字段名称(请向后端开发人员询问此字段的名称)

3) Append file type jpeg to uploading image name: 3)在上传图片名称后附加文件类型jpeg

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM