我想制作一个iPhone应用程序将图像发送到我的服务器.
我想在iPhone中绘制一些东西(例如:签名)作为图像,将二进制图像POST到我的服务器(服务器是JSP).请告诉我我该做什么?
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)
| 归档时间: |
|
| 查看次数: |
13296 次 |
| 最近记录: |