为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用Swift将图像上传到iOS中的服务器?

如何使用Swift将图像上传到iOS中的服务器?

郎朗坤 2019-12-03 10:51:11
它无法正常工作,如何将图像从iOS Swift应用发送到我的PHP服务器?@IBAction func upload(sender: UIButton) {    var imageData = UIImageJPEGRepresentation(img.image, 90)   // println(imageData)    let url = NSURL(string:"http://www.i35.club.tw/old_tree/test/uplo.php")    //let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData    //var request = NSMutableURLRequest(URL: url, cachePolicy: cachePolicy, timeoutInterval: 10)    var request = NSMutableURLRequest(URL: url)    request.HTTPMethod = "POST"    // set Content-Type in HTTP header    let boundaryConstant = "----------V2ymHFg03esomerandomstuffhbqgZCaKO6jy";    let contentType = "multipart/form-data; boundary=" + boundaryConstant    NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)    request.HTTPBody = imageData    // set data    //var dataString = "adkjlkajfdadf"    //let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)    //request.HTTPBody = requestBodyData    //    request.addValue(contentType, forHTTPHeaderField: "Content-Type")    request.addValue("multipart/form-data", forHTTPHeaderField: "Accept")    //    // set content length    //NSURLProtocol.setProperty(requestBodyData.length, forKey: "Content-Length", inRequest: request)    var response: NSURLResponse? = nil    var error: NSError? = nil    let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)    let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)    println("API Response: \(results)")}//take photofunc imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {    var selectedImage : UIImage = image     img.image = selectedImage    UIImageWriteToSavedPhotosAlbum(selectedImage, nil, nil, nil)    self.dismissViewControllerAnimated(true, completion: nil)}
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 845 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信