如何将图像发送到post方法?

时间:2015-09-16 10:47:51

标签: ios objective-c afnetworking

这里我是获取图像路径,我将路径转换为nsdata,但在服务器端接收空...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //You can retrieve the actual UIImage
    _signupImgView.image = [info valueForKey:UIImagePickerControllerOriginalImage];
    //Or you can get the image url from AssetsLibrary
    NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
    imageSending=[UIImage imageWithData:[NSData dataWithContentsOfURL:path]];
      imgdata = UIImagePNGRepresentation(imageSending);//here i am converting as nsdata...
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}

这是我的邮政编码

-(void)signupScreen:(NSString *)firstName :(NSString *)emailID :(NSString *)phoneNumber :(NSString *)password :(NSData *)img
{
    NSString *strSignupScreenRequest=[NSString stringWithFormat:@"firstname=%@&email=%@&telephone=%@&password=%@&pic=%@""%@",firstName,emailID,phoneNumber,password,img];
    NSData *dataFetch=[strSignupScreenRequest dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:localRegister]];
    [self requestFunction:url :dataFetch:NO];

}

这是我在全球范围内分配的帖子方法....

else
    {
        [request setURL:url];
        [request setHTTPMethod:@"POST"];

        NSString *conLength=[NSString stringWithFormat:@"%lu",(unsigned long)data.length];
        [request setValue:conLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-" forHTTPHeaderField:@"Content-Type"];

        [request setHTTPBody:data];
        NSLog(@"post method ===%@",request);
    }

我见过大多数人都说AFNETWORKING是好方法,因为很多文件都在那里我不知道如何实现,请任何人指导我....

2 个答案:

答案 0 :(得分:2)

这不是将数据传输到服务器的方法。您正在转换sudo ntpdate ntp.ubuntu.com中的所有内容,然后可能会发送它。

Here是你应该怎么做的。 This thread显示了如何发送图像数据以及其他详细信息。仔细阅读所有答案。

答案 1 :(得分:0)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

     [picker dismissViewControllerAnimated:YES completion:nil];
     SomeImageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}

图片现在在SomeImageView。您可以将其转换为NSData

 NSData *imageData =UIImageJPEGRepresentation(image, 1.0);

并使用multipart/form-data上传 如果您需要代码,请转到本教程。我建议的最好:http://nthn.me/posts/2012/objc-multipart-forms.html