1

I converted ipad signature to png image successfully using UIImagePNGRrepresentation(Image). Now I want to store this Image from swift to a SQL Server database using a web service. I have not any idea about how do this?

This is my swift code

UIGraphicsBeginImageContextWithOptions(self.signatureMainImageview.bounds.size, false, 0.0)
self.signatureMainImageview.image?.drawInRect(CGRectMake(0, 0, self.signatureMainImageview.frame.size.width, self.signatureMainImageview.frame.size.height))
let SaveImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let image = UIImagePNGRepresentation(SaveImage)

var CardDataObj = structCardData()

CardDataObj.CustomerSignature = image!

let requestCardData = NSMutableURLRequest(URL: NSURL(string: "http://URL")!)
requestCardData.HTTPMethod = "POST"
let postString =  CardDataObj.jsonRepresentation
requestCardData.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(requestCardData) {
    data, response, error in

    if error != nil {
        print("error=\(error)")
        return
    }
    print("response = \(response)")

    let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
    print("responseString = \(responseString)")
}

Now I want to know how to get this image in webservice? which datatype use in webservice for image? which datatype use in sql for image? How to send this image to sql?

New-Learner
  • 231
  • 1
  • 3
  • 15

1 Answers1

1
  1. Rather than a data task you need an upload task. Either uploadTaskWithRequest:fromData:completionHandler or its file or stream variants
  2. In order to begin the task you need to call task.resume()

It also helps to retrieve the response if you cast to HTTPURLResponse like so:

if let response = response as? NSHTTPURLResponse {
    response.statusCode
    response.allHeaderFields
}

I wrote a blogpost on uploading using a stream, which might be of some use. Here's also a more general post about NSURLSession.

The first blogpost linked to will give you some server-side code in PHP to receive a stream, but if you are uncertain about what to do on the SQL I'd recommended breaking this question into two and asking that question separately.

sketchyTech
  • 5,137
  • 28
  • 52
  • This is not related to my answer. I want to store signature image on my sql database using webservice – New-Learner Apr 13 '16 at 10:13
  • In order to do that you need to know how to upload the image from your app. As stated above this is a two-part question that should be broken into two – sketchyTech Apr 13 '16 at 10:14
  • CustomerSignature is a variable and that datatype is NSData – New-Learner Apr 13 '16 at 10:14
  • What happens in `jsonRepresentation`? I'm assuming base64 conversion of the image so that it can be contained within the json. – sketchyTech Apr 13 '16 at 10:21
  • Discussion of [how to store images](http://stackoverflow.com/questions/9722603/storing-image-in-database-directly-or-as-base64-data) and for the actual insertion of data into a mysql database you need to look at tutorials preferably targeted at whichever server-side language you are using – sketchyTech Apr 13 '16 at 10:29
  • You mean I upload image first using uploadTaskWithRequest:fromData:completionHandler on server – New-Learner Apr 13 '16 at 10:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109028/discussion-between-new-learner-and-sketchytech). – New-Learner Apr 13 '16 at 10:33