Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we use imageData to post to server #1

Open
ozgrozer opened this issue Jan 29, 2017 · 2 comments
Open

How can we use imageData to post to server #1

ozgrozer opened this issue Jan 29, 2017 · 2 comments

Comments

@ozgrozer
Copy link

I'm new in Swift and in the saveToCamera() function I just wanna send the captured image to the server. I tried to send the picture with Alamofire library and I couldn't. Which constant should I post? 'imageData', 'cameraImage' or do I need to convert those something else?

@adarshvcdev
Copy link
Owner

Try this sample code.

Alamofire.upload(
.POST,
"your_API_URL_here",
multipartFormData: { multipartFormData in
if let _ = image {
if let imageData = UIImageJPEGRepresentation(image!, 1.0) {
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.jpeg", mimeType: "image/jpeg")
} else {
print(image)
}
}
}, encodingCompletion: {
encodingResult in

        switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON { response in
                    switch response.result {
                        case .Success:
                            print("Success")
                        case .Failure(let error):
                            print(error)
                        }
                    }
            case .Failure(let encodingError):
                print(encodingError)
            }
        }
    )
}

} catch _ {

}

@ozgrozer
Copy link
Author

ozgrozer commented Feb 5, 2017

Thanks for the answer but I didn't understand this line.
let imageData = UIImageJPEGRepresentation(image!, 1.0)

What is that image variable? In your saveToCamera function there are two variables named let imageData and let cameraImage.

Do you mean
let imageData = UIImageJPEGRepresentation(cameraImage!, 1.0)
or anything else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants