如何使用webservice将图像从swift保存到SQL Server数据库?

时间:2021-10-04 16:34:29

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?

我使用UIImagePNGR表示(图像)将ipad签名成功转换为png图像。现在我想使用Web服务将此图像从swift存储到SQL Server数据库。我不知道这是怎么做到的?

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?

现在我想知道如何在webservice中获取此图像?哪个数据类型在webservice中用于图像?哪个数据类型在sql中用于图像?如何将此图像发送到sql?

1 个解决方案

#1


1  

  1. Rather than a data task you need an upload task. Either uploadTaskWithRequest:fromData:completionHandler or its file or stream variants
  2. 您需要上传任务,而不是数据任务。 uploadTaskWithRequest:fromData:completionHandler或其文件或流变体

  3. In order to begin the task you need to call task.resume()
  4. 要开始任务,您需要调用task.resume()

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

如果您像这样转换为HTTPURLResponse,它也有助于检索响应:

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.

我写了一篇关于使用流上传的博客文章,这可能有些用处。这里还有一篇关于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.

链接到的第一个博客文章将为您提供PHP中的一些服务器端代码以接收流,但如果您不确定如何对SQL执行操作,我建议将此问题分解为两个并分别提出该问题。

#1


1  

  1. Rather than a data task you need an upload task. Either uploadTaskWithRequest:fromData:completionHandler or its file or stream variants
  2. 您需要上传任务,而不是数据任务。 uploadTaskWithRequest:fromData:completionHandler或其文件或流变体

  3. In order to begin the task you need to call task.resume()
  4. 要开始任务,您需要调用task.resume()

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

如果您像这样转换为HTTPURLResponse,它也有助于检索响应:

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.

我写了一篇关于使用流上传的博客文章,这可能有些用处。这里还有一篇关于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.

链接到的第一个博客文章将为您提供PHP中的一些服务器端代码以接收流,但如果您不确定如何对SQL执行操作,我建议将此问题分解为两个并分别提出该问题。