将图像作为二进制数据发送到服

Mar*_*Joo 5 iphone

我想制作一个iPhone应用程序将图像发送到我的服务器.

我想在iPhone中绘制一些东西(例如:签名)作为图像,将二进制图像POST到我的服务器(服务器是JSP).请告诉我我该做什么?

  • 如何使用iPhone UI?
  • 如何从图像等制作二进制数据

Yan*_*iot 13

首先,您可以使用UIImagePNGRepresentation和UIImageJPEGRepresentation函数获取包含图像数据的PNG或JPEG表示的NSData对象.

// To get the data from a PNG file
NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);

// To get the data from a JPEG file
NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);
Run Code Online (Sandbox Code Playgroud)

(有关更多信息,请参阅:UIImage类参考)

要完成将数据从iPhone上传到服务器,您可以执行以下操作:

- (void)sendImage {
       NSData *postData = [nsdata from your original image];
       NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

       // Init and set fields of the URLRequest
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
       [request setHTTPMethod:@"POST"];
       [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
       [request setHTTPBody:postData];

       NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
       if (connection) {
          // Return data of the request
          NSData *receivedData = [[NSMutableData data] retain];
       }
       [request release];
 }
Run Code Online (Sandbox Code Playgroud)

  • 如果您还想在邮件请求中在服务器上设置几个变量,该怎么办? (2认同)