从iPhone发布到服务器时,图像已损坏

时间:2012-12-05 08:39:11

标签: php iphone web-services

这是我的代码将图像上传到php服务器,但是图像作为损坏的图像上传到服务器。我的代码请告诉我问题..我已经尝试过cocoa rest客户端,图像正在从中上传。< / p>

    NSString *filename = @"connect-blue.png";
    UIImage *image = [UIImage imageNamed:filename];
    NSMutableURLRequest * request= [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;    boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profile_image\"; filename=\"%@\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
   [postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [postbody appendData:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    NSData *imagedata = UIImageJPEGRepresentation(image,1.0f);

    [postbody appendData:[NSData dataWithData:imagedata]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary]  dataUsingEncoding:NSUTF8StringEncoding]];

Php代码在这里

if(!empty($_FILES)){
//
  $imgname=$_FILES['image']['name'];
         $a=pathinfo($imgname);

  if(!empty($a['extension'])){  
   $img_name=$a['filename'];
   $img_ext=$a['extension'];
 }

  else

  {

    $img_name='';

   $img_ext='';

   }

   $img_pre=mktime();

   $image1=$img_pre."_".$imgname;
  move_uploaded_file($_FILES['image']['tmp_name'],  "uploads/images/" .  $image1);

2 个答案:

答案 0 :(得分:2)

我找到了解决方案Image Uploading

答案 1 :(得分:0)

您可以管理这两种技术的图像框架 我的意思是当你从iphone发送图像时,图像的大小在php中仍然是相同的 我认为你管理Async Image类