照片发布到服务器从iPhone应用程序

时间:2014-03-03 14:17:28

标签: ios iphone objective-c api

我从ios应用上传了一张图片。当我使用UIImagePickerController从我的设备中选择图像时,图像质量会很好。

enter image description here

现在我使用此代码将此图像发布到服务器中。

NSString *urlAsString = @"http://0.0.0.0/postwebs/AddPost/";
  urlAsString = [urlAsString stringByAppendingString:[NSString stringWithFormat:@"?user_id=%@",userId]];//,str_UserID]];
  urlAsString = [urlAsString stringByAppendingString:[NSString stringWithFormat:@"&session_id=%@",sessionId]];//str_SessionID]];
  urlAsString = [urlAsString stringByAppendingString:[NSString stringWithFormat:@"&post_title=%@",str_PostTitle]];
  urlAsString = [urlAsString stringByAppendingString:[NSString stringWithFormat:@"&mediatype=photo"]];
  urlAsString = [urlAsString stringByAppendingString:[NSString stringWithFormat:@"&post_location=%@",[MASingleton getLocation]]];
  urlAsString = [urlAsString stringByAppendingString:[NSString stringWithFormat:@"&hash_tag=%@",hashTag]];
NSString *properlyEscapedURL = [urlAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  NSURL *url = [NSURL URLWithString:properlyEscapedURL];


  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  [request setURL:url];
  [request setHTTPMethod:@"POST"];

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


  NSMutableData *body = [NSMutableData data];
  [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"post_mediafile\"; filename=\"test2.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[NSData dataWithData:data_PostingImage]];
  [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [request setHTTPBody:body];

图片已在服务器中成功发布。现在我想从服务器检索此图像现在图像质量

enter image description here

我使用此

从服务器获取图像
dispatch_async(queue, ^{
          NSString *str_ImageUrl = [NSString stringWithFormat:@"http://%@",[userImageArr objectAtIndex:indexPath.row]];
          NSURL *url_Image = [NSURL URLWithString:str_ImageUrl];
          NSData *imgData = [NSData dataWithContentsOfURL:url_Image];
          if (imgData) {
           UIImage *image = [UIImage imageWithData:imgData];
           if (image) {
            dispatch_async(dispatch_get_main_queue(), ^
            {
             cell.img_ToUserImage.image = image;
            });
           }
          }
         });

我在这段代码中犯了什么错误。

0 个答案:

没有答案