如何将图像发布到Web服务

时间:2012-12-27 10:01:44

标签: iphone objective-c ios xcode

在我的项目中: -

1)点击相机中的图片(不要保存)。

2)显示一个裁剪的图像,其中有2个按钮发布并取消。其中发布按钮后发布图像,取消按钮丢弃图像。看起来像这样。 2nd

我不知道如何发布相机捕获图像。我在互联网上搜索了很多代码,但没有找到任何帮助我的代码。

我尝试了在这些链接上给出的代码..

FirstSecondThird以及其他许多人。

我希望有人能帮助......如何发布?感谢。

-(IBAction)sendRequest:(id)sender {

     #define DataDownloaderRunMode @"myapp.run_mode" 

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url 
                                       cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                       timeoutInterval:60]; 

        [request setHTTPMethod:@"POST"]; 

        NSString *boundary = @"----WebKitFormBoundarycC4YiaUFwM44F6rT";

        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 stringWithString:@"Content-Disposition: form-data; name="attachment[file]";
        filename="picture.png"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[[NSString stringWithString:@"Content-Type: image/png\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];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request 
                                                               delegate:self 
                                                               startImmediately:NO]; 

        [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:DataDownloaderRunMode]; 

        [connection start];
}

并将其连接到帖子按钮..

2 个答案:

答案 0 :(得分:3)

<强>更新

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"picture\"; filename=\"picture.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

此处用于存储图像的数据库字段名称为picture,然后在name参数中写入字段名称...

使用此代码...

答案 1 :(得分:2)

// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}  

按照此回答Link

或者您可以将ASIHTTP类用于此

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://URL/Service.asmx/RegisterClient?"]];
            request.delegate=self;

            encodedImage = UIImagePNGRepresentation(imgRef);

            [[NSUserDefaults standardUserDefaults]setObject:UIImagePNGRepresentation(imgRef) forKey:@"userImage"];

            str =[Base64 encode:encodedImage];

           [request setPostValue:email.text forKey:@"Email"];

            [request setPostValue:name.text forKey:@"FName"];
            [request setPostValue:str forKey:@"UserImage"];
            [request setPostValue:dob.text forKey:@"dob"];
            [request setPostValue:phoneNumber.text forKey:@"Phone"];
            [request setPostValue:ggg.text forKey:@"Gender"];
            [request setPostValue:address.text forKey:@"Address"];
            [request setPostValue:interest.text forKey:@"Interest"];

            [request startAsynchronous];

ASIHTTPRequest documentation