SWIFT:使用PHP将相机图片保存到服务器,从而提供空的POST阵列

时间:2015-05-13 19:58:26

标签: php ios swift upload

我正在尝试从相机中保存图像。现在我只使用UIImagePickerController()sourceType = .SavedPhotosAlbum在iOS模拟器上进行测试。

我遇到的问题是当我将图像保存/上传到服务器时,我得到一个空的$ _FILES和$ _POST数组。我检查了我的php.ini文件,一切都在那里设置。

当我包含这行代码并打印变量image时,我在控制台中得到nil。但是当我删除它时,我会得到一长串的数字

func postImage(image:UIImage, completionHandler: ErrorCompletionHandler)
{
    //let image = UIImage(named: "image.jpg")

    var imageData = UIImageJPEGRepresentation(image,0.5) // returns NSData Objects
    // println(imageData)
    if imageData != nil{
        var request = NSMutableURLRequest(URL: NSURL(string:"http://www.example.com/upload")!)
        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 as String, 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

        println(body)

        var returnData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
        var returnString = NSString(data: returnData!, encoding: NSUTF8StringEncoding)

        println("returnString \(returnString)")

    }
}


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])
{
    var image: UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    println(image)
    println("Image Picked")


    self.postImage(image, completionHandler: {
        (error) -> () in

        if let constError = error
        {
            self.showAlert("Error", message: constError.localizedDescription)
        }


    })

}

我的swift和PHP代码如下:

var_dump($_POST);exit;

我的PHP代码只是

<UIImage: 0x7fbad91598e0> size {1280, 850} orientation 0 scale 1.000000

当我单击默认图像网格中的图像时,我打印变量,图像输出,我得到

var http = require('http');

http.createServer(function(request, response) {
    response.writeHead(200);
    response.write("Hey, I am a dinosaur!");
    response.end();
}).listen(8080);
console.log("Listening on Port 8080");

但我从PHP var_dump中得到一个空数组。我做错了吗?

0 个答案:

没有答案