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

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

本文介绍了将图像作为二进制数据发送到服的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个iPhone应用程序来将图像发送到我的服务器。

I want to make an iPhone application to send an image to my server.

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

I want to draw something in iPhone (ex: a signature) as an image to POST binary image to my server (server is JSP). Please tell me what I have to do?


  • 如何使用iPhone UI?

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

推荐答案

首先,你可以获得一个包含任何一个的NSData对象使用UIImagePNGRepresentation和UIImageJPEGRepresentation函数的图像数据的PNG或JPEG表示。

Firstly you can get an NSData object containing either a PNG or JPEG representation of the image data using the UIImagePNGRepresentation and UIImageJPEGRepresentation functions.

// 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);

(有关详细信息,请参阅:)

(for more information see: UIImage Class Reference)

完成将数据从iPhone上传到服务器即可:

To finish to upload data from your iPhone to your server you can do this:

- (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];
 }

这篇关于将图像作为二进制数据发送到服的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:21