拍摄相机照片并将其发送到iPhone中的php服务器

时间:2013-12-11 10:21:23

标签: php ios web-services

我正在开发类似的iphone应用程序。根据我的要求,我需要使用php网址将相机捕获的图像发送到服务器。我将捕获的图像存储在应用程序资源文档目录中。

这是我的Php代码 -

$file_path = "../Gallery/";

   $file_path = $file_path . basename( $_FILES['Documents']['name']);
   if(move_uploaded_file($_FILES['Documents']['tmp_name'], $file_path)) {
       echo "Image is upload";
   } else{
       echo "Image is not Upload";
   }

这是我的iPhone代码 -

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    CGFloat compression = 0.5f;

    UIImage *smallSizeImage = [self scaleImage:image toSize:CGSizeMake(140.0, 80.0)];
    imageData = UIImageJPEGRepresentation(smallSizeImage, compression);

    ivPicture.image = smallSizeImage;

    self.passingNbPosition.positionImageId = [Util getNewGUID];

    [imageData writeToFile:[Util getFilePathForFileName:[NSString stringWithFormat:@"%@.jpg",self.passingNbPosition.positionImageId]] atomically:YES];


   // setting up the URL to post to
    NSString *urlString = @"http://...........php";

    // setting up the request object now
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", self.passingNbPosition.positionImageId] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(returnString);

}

在控制台中打印后使用上面的代码,我得到返回数据,但返回字符串即将出现“图像不上传”。我不知道我的错误在哪里。我认为这是我的iPhone代码中的一个问题。有人可以帮助我,我在这里做错了。

谢谢..

3 个答案:

答案 0 :(得分:1)

- (void) saveImageInServer
{
    /*
     turning the image into a NSData object
     getting the image back out of the UIImageView
     setting the quality to 90
     */
        NSData *imageData = UIImageJPEGRepresentation(self.profilePicture.image, 90);
        // setting up the URL to post to
        NSString *urlString = @"http://webservices/image-upload.php";

        // setting up the request object now
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        /*
         add some header info now
         we always need a boundary when we post a file
         also we need to set the content type

         You might want to generate a random boundary.. this is just the same
         as my output from wireshark on a valid html post
         */
        NSString *boundary = @"---------------------------147378098314876653456641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

        /*
         now lets create the body of the post
         */
        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *stringData = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\%@.jpg\r\n",self.emailTextField.text];
        [body appendData: [stringData dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // setting the body of the post to the reqeust
        [request setHTTPBody:body];


        // now lets make the connection to the web
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        NSLog(@"%@",returnString);


}

这是应用程序中的代码,正如我告诉你的那样,检查服务器中的Gallery文件夹,如果它存在并指向它,它将起作用

答案 1 :(得分:0)

如果您想以multipart / form-data发送图片,我们有第三方API,即ASIHttpRequest。通过以下链接,您就会知道如何使用它。

http://allseeing-i.com/ASIHTTPRequest/How-to-use

答案 2 :(得分:0)

我没有在您的代码中看到任何错误,可能是解决此问题的一些建议:

  1. 以不同的方式选择和上传图片
  2. 检查您的服务器是否确实在正确的路径中有一个名为Gallery的文件夹,否则它将无效(尝试自己创建一个,并写下它的绝对路径)