0

My app is used uiwebview to reload the page, but page on a function of the camera to upload pictures, I wouldn't use uiwebview upload pictures, I got the following code on the Internet, but it should not be used. I don't know how to do and need your help, thank you

- (void)beginSendImage:(UIImage *)image
{
//把图片转换成imageDate格式
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

//传送路径
    NSString *urlString = ShareManager.systemUrl;
    if([urlString rangeOfString:@"http"].location == NSNotFound){
        urlString = [NSString stringWithFormat:@"http://%@/JBIZ/AppUploadAction",urlString];
    }else{
        urlString = [NSString stringWithFormat:@"%@/JBIZ/AppUploadAction",urlString];
    }



//建立请求对象
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
//设置请求路径
[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 *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


[body appendData:[@"Content-Disposition: form-data; name=\\userfile\\; filename=\vim_go.jpg\\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n"dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
//上传文件开始
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//获得返回值
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);




}
Mr yang
  • 3
  • 1

1 Answers1

0

Can you give us more details about your problem, please?

Here is a good solution to send an image with HTTP POST to a server : Upload image to the PHP server from iOS

And you can use Runscope to intercept and debug your HTTP requests, it is a very useful tool!

Community
  • 1
  • 1
B 7
  • 670
  • 7
  • 23
  • 1
    For iOS specific examples of sending your HTTP traffic through Runscope (NSUrlConnection & AFNetworking) - https://www.runscope.com/docs/code-examples/ios – mansilladev Aug 13 '15 at 18:53