¿Cómo subir imágenes a un servidor en iOS con Swift?

No funcionará, ¿cómo enviar una imagen desde una aplicación iOS Swift a mi servidor 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 photo func 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) } 

He tomado la foto por elección del usuario y código escrito para swift 2 y iOS9

  func imageUploadRequest(imageView imageView: UIImageView, uploadUrl: NSURL, param: [String:String]?) { //let myUrl = NSURL(string: "http://192.168.1.103/upload.photo/index.php"); let request = NSMutableURLRequest(URL:uploadUrl); request.HTTPMethod = "POST" let boundary = generateBoundaryString() request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") let imageData = UIImageJPEGRepresentation(imageView.image!, 1) if(imageData==nil) { return; } request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData!, boundary: boundary) //myActivityIndicator.startAnimating(); let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in if let data = data { // You can print out response object print("******* response = \(response)") print(data.length) // you can use data here // Print out reponse body let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) print("****** response data = \(responseString!)") let json = try!NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? NSDictionary print("json value \(json)") //var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &err) dispatch_async(dispatch_get_main_queue(),{ //self.myActivityIndicator.stopAnimating() //self.imageView.image = nil; }); } else if let error = error { print(error.description) } }) task.resume() } 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 = "user-profile.jpg" let mimetype = "image/jpg" 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)" } }// extension for impage uploading extension NSMutableData { func appendString(string: String) { let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) appendData(data!) } } 

——— código siguiente para el lado php

 < ?php $firstName = $_POST["firstName"]; $lastName = $_POST["lastName"]; $userId = $_POST["userId"]; $target_dir = "media"; if(!file_exists($target_dir)) { mkdir($target_dir, 0777, true); } $target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]); if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) { echo json_encode([ "Message" => "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.", "Status" => "OK", "userId" => $_REQUEST["userId"] ]); } else { echo json_encode([ "Message" => "Sorry, there was an error uploading your file.", "Status" => "Error", "userId" => $_REQUEST["userId"] ]); } 

Para cargar imágenes con parámetros, use este código,

  // Your method to upload image with parameters to server. func uploadImageOne(){ var imageData = UIImagePNGRepresentation(imageView.image) if imageData != nil{ var request = NSMutableURLRequest(URL: NSURL(string:"Enter Your 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)") } } 

Yo recomendaría echar un vistazo a: https://github.com/sraj/Swift-SRWebClient

Función de muestra que funcionó para mí:

 func uploadImage(image:UIImage) { let imageData:NSData = UIImageJPEGRepresentation(image, 100) SRWebClient.POST("http://www.example.com/upload/") .data(imageData, fieldName:"photo", data: ["foo":"bar","baz":"qux"]) .send({(response:AnyObject!, status:Int) -> Void in // process success response },failure:{(error:NSError!) -> Void in // process failure response }) } 

Notará que el código anterior es una versión ligeramente ajustada de los fragmentos en la página Swift-SRWebClient Github. NSData.dataWithData con UIImageJPEGRepresentation porque el primero está en desuso.

Además, no olvides arrastrar SRWebClient.swift a tu carpeta de proyecto, de lo contrario, esto no funcionará 🙂

Mi código actualizado para Swift 3

 func uploadImage() { let url = URL(string: "YOUR SERVER URL"); let request = NSMutableURLRequest(url: url!); request.httpMethod = "POST" let boundary = "Boundary-\(NSUUID().uuidString)" request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") var retreivedImage: UIImage? = nil //Get image do { let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String let readData = try Data(contentsOf: URL(string: "file://\(documentsPath)/myImage")!) retreivedImage = UIImage(data: readData) addProfilePicView.setImage(retreivedImage, for: .normal) } catch { print("Error while opening image") return } let imageData = UIImageJPEGRepresentation(retreivedImage!, 1) if (imageData == nil) { print("UIImageJPEGRepresentation return nil") return } let body = NSMutableData() body.append(NSString(format: "\r\n--%@\r\n", boundary).data(using: String.Encoding.utf8.rawValue)!) body.append(NSString(format: "Content-Disposition: form-data; name=\"api_token\"\r\n\r\n" as NSString).data(using: String.Encoding.utf8.rawValue)!) body.append(NSString(format: (UserDefaults.standard.string(forKey: "api_token")! as NSString)).data(using: String.Encoding.utf8.rawValue)!) body.append(NSString(format: "\r\n--%@\r\n", boundary).data(using: String.Encoding.utf8.rawValue)!) body.append(NSString(format:"Content-Disposition: form-data; name=\"profile_img\"; filename=\"testfromios.jpg\"\r\n").data(using: String.Encoding.utf8.rawValue)!) body.append(NSString(format: "Content-Type: application/octet-stream\r\n\r\n").data(using: String.Encoding.utf8.rawValue)!) body.append(imageData!) body.append(NSString(format: "\r\n--%@\r\n", boundary).data(using: String.Encoding.utf8.rawValue)!) request.httpBody = body as Data let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in if let data = data { // do what you want in success case } else if let error = error { print(error.localizedDescription) } }) task.resume() } 

Y el código del lado de PHP que puedes tomar de @VinodJoshi answer.

prueba así

 @IBAction func upload(sender: UIButton) { var imageData = UIImageJPEGRepresentation(img.image, 90) var url = NSURL(string: "http://www.i35.club.tw/old_tree/test/uplo.php") var request = NSMutableURLRequest(URL: url) request.HTTPMethod = "POST" request.HTTPBody = NSData.dataWithData(UIImagePNGRepresentation(imageData)) 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)") } 

referir este enlace

Esto es genial, he estado tratando de pasar una imagen usando REST por un tiempo y no he podido formatearla correctamente y seguí recibiendo errores de tiempo de espera. Usando createBodyWithParameters pude hacerlo funcionar.

Aquí está la versión Swift 3 del código:

 func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: UIImage, boundary: String) -> Data { var body = Data(); if parameters != nil { for (key, value) in parameters! { body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!) body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: String.Encoding.utf8)!) body.append("\(value)\r\n".data(using: String.Encoding.utf8)!) } } let filename = "user-profile.jpg" let mimetype = "image/jpg" body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!) body.append("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n".data(using: String.Encoding.utf8)!) body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!) body.append(UIImageJPEGRepresentation(imageDataKey, 1)!) //body.appendData(imageDataKey) body.append("\r\n".data(using: String.Encoding.utf8)!) body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!) return body } 

He hecho funcionar para el método Block que es fácil de usar y esta es la función de clase para que pueda usar en cualquier lugar de su proyecto, por ejemplo, hacer

 MVCServer.swift 

Clase NSObject y

 import Alamofire 

y luego escribe esta función en esta clase

  func postImageRequestWithURL(withUrl strURL: String,withParam postParam: Dictionary,withImages imageArray:NSMutableArray,completion:@escaping (_ isSuccess: Bool, _ response:NSDictionary) -> Void) { Alamofire.upload(multipartFormData: { (MultipartFormData) in // Here is your Image Array for (imageDic) in imageArray { let imageDic = imageDic as! NSDictionary for (key,valus) in imageDic { MultipartFormData.append(valus as! Data, withName:key as! String,fileName: "file.jpg", mimeType: "image/jpg") } } // Here is your Post patwigters for (key, value) in postParam { MultipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key) } }, usingThreshold: UInt64.init(), to: strURL, method: .post) { (result) in switch result { case .success(let upload, _, _): upload.uploadProgress(closure: { (progress) in print("Upload Progress: \(progress.fractionCompleted)") }) upload.responseJSON { response in if response.response?.statusCode == 200 { let json = response.result.value as? NSDictionary completion(true,json!); } else { completion(false,[:]); } } case .failure(let encodingError): print(encodingError) completion(false,[:]); } } } 

Ahora llama a esta función

 var ImageArray : NSMutableArray! // Here is your post parameter var parameters: [String:Any] = ["user_master_id" : “56”, "first_name": “Jignesh”, "last_name" : “Mayani”, "email": “TestEmail@gmail.com”] // Here is your image is in DATA formate don't send as UIImage formate let ImageDic = ["profile_image" : imageData!] // Here you can pass multiple image in array i am passing just one ImageArray = NSMutableArray(array: [ImageDic as NSDictionary]) MVCServer().postImageRequestWithURL(withUrl: "write Your URL here", withParam: parameters, withImages: ImageArray) { (isSuccess, response) in // Your Will Get Response here } 
 func imageUpload(imageData:String){ var request:NSMutableURLRequest! let param:String = "" request = NSMutableURLRequest(url: URL(string: "www.pickmyoffers.com")!)// use your upload php file link and parameter "example.com/upload.php?image=\(imageData)" request.httpMethod = "POST" request.httpBody = param.data(using: String.Encoding.utf8) let session = URLSession.shared let task = session.dataTask(with: request as URLRequest) {data,response,error in if error != nil { print("error=\(error)") return } let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) print(responseString!) } task.resume() }