我正在研究iphone应用程序库。根据我的要求,我需要使用php url将摄像机捕获的图像发送到服务器。我将捕获的图像存储在应用程序资源文档目录中。

这是我的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代码中的问题。有人可以帮我在这里我做错了什么。

谢谢..

最佳答案

- (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文件夹,如果该文件夹存在并且您指向该文件夹,则它将起作用

关于php - 拍摄相机图片并将其发送到iPhone中的php服务器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20516465/

10-11 14:11
查看更多