将签名上传到服务器

时间:2016-03-29 09:50:22

标签: ios swift

在应用程序中,我创建了UIImage的签名,并将图像存储到iPhone相机胶卷中,现在我需要将签名文件上传到服务器中,并使用" sign"作为参数。我使用以下代码

@IBAction func saveSignature(sender: AnyObject) {
    // Getting the Signature Image from self.drawSignatureView using the method getSignature().
    let signatureImage = self.drawSignatureView.getSignature() //signatureImage is UIImage

    // API
    let myUrl = NSURL(string: "http://moneymonkey.tokiiyo.com/api/signature");

    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    let param = [
        "api_key"  : "AiK58j67",
        "api_secret"    : "a#9rJkmbOea90-",
        "phone"    : "\(mobile)",
        "sign"  : ""     //Here what string need to pass as parameter
    ]

    let boundary = generateBoundaryString()

    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    let imageData = UIImagePNGRepresentation(signatureImage)

    if(imageData==nil)  { return; }

    request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData!, boundary: boundary)   //here what does file means

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

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

        // You can print out response object
        print("******* response = \(response)")

        // Print out response body
        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("****** response data = \(responseString!)")
        do{

            var json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
            dispatch_async(dispatch_get_main_queue(),{
            });
        }
        catch
        {

            // report error
            print("Oops!! Something went wrong\(error)")
        } 
    }

    task.resume()

    // Saving signatureImage from the line above to the Photo Roll.
    // The first time you do this, the app asks for access to your pictures.
    UIImageWriteToSavedPhotosAlbum(signatureImage, nil,
        nil, nil)
    print("Signature Image:\(signatureImage)")  
}

func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {
    let body = NSMutableData();

    if parameters != nil {
        for (key, value) in parameters! {
            body.appendString("--\(boundary)\r\n")
            body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
            body.appendString("\(value)\r\n")
        }
    }

    let filename = "signature.png"

    let mimetype = "image/png"

    body.appendString("--\(boundary)\r\n")
    body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
    body.appendString("Content-Type: \(mimetype)\r\n\r\n")
    body.appendData(imageDataKey)
    body.appendString("\r\n")



    body.appendString("--\(boundary)--\r\n")

    return body
}

func generateBoundaryString() -> String {
    return "Boundary-\(NSUUID().UUIDString)"
}

我是否走在正确的道路上。请任何人建议我正确的方法。提前致谢

0 个答案:

没有答案