我正在开发一个iOS应用程序,我必须将图像上传到服务器,我正在使用swift 2.0。 以下是代码:
var tempImage:UIImage = info [UIImagePickerControllerOriginalImage] as!的UIImage myImage.image = tempImage
print(tempImage)
print([info])
myImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
var imageData = UIImagePNGRepresentation(tempImage)
// converting in base64
let base64 = imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) as! AnyObject
let url = NSURL(string: "www.google.com/myimage1/PNG")!
let urlRequest = NSMutableURLRequest(URL: url)
urlRequest.HTTPMethod = "POST"
// Set the HTTP Body with the POST data
let postDict = ["encodedImage": base64]
urlRequest.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(postDict , options: [])
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.timeoutInterval = 600
urlRequest.HTTPShouldHandleCookies = false
// urlRequest.setValue("application/json)", forHTTPHeaderField: "Content-Type")
let task = NSURLSession.sharedSession().dataTaskWithRequest(urlRequest)
{
data, response, error in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
但我还没有成功。我仍然得到以下错误响应。
error = Optional(错误域= NSURLErrorDomain代码= -1001"请求 超时。" UserInfo = {NSUnderlyingError = 0x7fad5af61e20 {错误 Domain = kCFErrorDomainCFNetwork Code = -1001"(null)" UserInfo = {_ kCFStreamErrorCodeKey = 60,_kCFStreamErrorDomainKey = 1}}, NSErrorFailingURLStringKey = www.google.com / myimage1 / PNG, NSErrorFailingURLKey = http://www.google.com/myimage1/PNG, _kCFStreamErrorDomainKey = 1,_kCFStreamErrorCodeKey = 60,NSLocalizedDescription =请求超时。})
如果您已经从swift 2.0中的应用程序上传了服务器上的图像,请帮助我。
答案 0 :(得分:1)
您需要更改文件
info.plist
,并使用应用TextEdit添加,或使用xcode。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
来自Xcode的http://i.stack.imgur.com/tGsEK.png
,不要忘记清理项目。 (cmd + shift + k)
答案 1 :(得分:0)
func uploadImageOne()
{
var imageData = UIImagePNGRepresentation(imageView.image)
if imageData != nil{
var request = NSMutableURLRequest(URL: NSURL(string:"= Enter your image URL")!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var boundary = NSString(format: "---------------------------14737809831466499882746641449")
var contentType = NSString(format: "multipart/form-data; boundary=%@",boundary)
// println("Content Type \(contentType)")
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
var body = NSMutableData.alloc()
// Title
body.appendData(NSString(format: "\r\n--%@\r\n",boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Disposition: form-data; name=\"title\"\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
// Image
body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Disposition: form-data; name=\"profile_img\"; filename=\"img.jpg\"\\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format: "Content-Type: application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(imageData)
body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPBody = body
var returnData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
var returnString = NSString(data: returnData!, encoding: NSUTF8StringEncoding)
println("returnString \(returnString)")
}