为啥我无法正确获取图像数据或无法将数据发送到服务器?

Posted

技术标签:

【中文标题】为啥我无法正确获取图像数据或无法将数据发送到服务器?【英文标题】:Why i am not getting image data properly or unable to send data to server?为什么我无法正确获取图像数据或无法将数据发送到服务器? 【发布时间】:2016-12-28 10:26:31 【问题描述】:

我正在尝试在我的UIViewController 中显示来自服务器端的所有数据。我在我的项目中使用AFNetworking。我将使用POST 方法将参数发送到服务器。我得到了服务器的响应,我能够显示所有数据,例如:姓名、电子邮件、wellness_id、手机和图像。我得到了图像 URL,并将其显示在 UIImageView 中。

我的要求 - 当用户单击编辑图像按钮时,他可以从设备中选择图像并将更改发送到服务器。

我做了什么:当用户点击编辑图片按钮时,我可以从设备中获取图片,并且可以在UIImageView中设置。

我的问题是,为什么它不向服务器发送任何数据??意味着我可以从设备中设置UIImageView 中的图像,但是NSLog(@"imgdata:%@",imgdata); 这不是被调用的???? 请在这个问题上帮助我。我试过这样:

[manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) 

    NSLog(@"Response from server  :  %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    NSError *error;

    NSArray *temp = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];

    _exceptionstr = [[temp objectAtIndex:0] valueForKey:@"exception"];
    _wellnessidstr = [[temp objectAtIndex:0] valueForKey:@"wellness_id"];
    _namestr = [[temp objectAtIndex:0] valueForKey:@"name"];
    _emailstr = [[temp objectAtIndex:0] valueForKey:@"email_id"];
    _mobilestr = [[temp objectAtIndex:0] valueForKey:@"mobile_no"];

    _imagestr=[[temp objectAtIndex:0]valueForKey:@"profile_pic"];

    NSString *imgURL = [NSString stringWithFormat:@"%@",_imagestr];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];

    dispatch_async(dispatch_get_main_queue(), ^
        [_image setImage:[UIImage imageWithData:data]];
    );


    NSLog(@"abcde:::: %@,%@,%@,%@,%@",_namestr,_wellnessidstr,_emailstr,_mobilestr,_imagestr);

    _name.text=[NSString stringWithString:_namestr];
    NSLog(@"%@",_name.text);


    _mobile.text=[NSString stringWithFormat:@"%@",_mobilestr];

    _email.text=[NSString stringWithString:_emailstr];
    NSLog(@"%@",_email.text);

    _wellness_id.text=[NSString stringWithString:_wellnessidstr];
    NSLog(@"%@",_wellness_id.text);

用于从设备获取图像并将更改发送到服务器:

- (IBAction)changeImage:(id)sender 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;

    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    imagePickerController.delegate = self;

    [self presentViewController:imagePickerController animated:YES completion:nil];




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

    UIImage *imageimg = [info valueForKey:UIImagePickerControllerOriginalImage];

    [_image setImage:imageimg];

    NSData *imgdata = UIImagePNGRepresentation(_image.image);

    NSLog(@"imgdata:%@",imgdata);

    AppDelegate *del = [[UIApplication sharedApplication] delegate];


    NSString *Loginurl = [NSString stringWithFormat:@"%@/EditProfiles/Pic",del.Root_URL];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *t = [prefs stringForKey:@"token"];
    NSLog(@"%@",t);


    NSDictionary *params = @@"token":t,

                             @"profile_pic":imgdata

                             ;

    //here we can see parameters which is sent to server


    NSLog(@"Sent parameter to edit image server : %@",params);



    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];


    manager.responseSerializer = [AFHTTPResponseSerializer serializer];


    AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

    [policy setValidatesDomainName:NO];

    [policy setAllowInvalidCertificates:YES];


    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",nil];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",nil];



    [manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) 


        // Here we can see response which is coming from server

        NSLog(@"Response from server  :  %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

【问题讨论】:

my problem is, why it not send any data to server?? 请澄清这一点。 表示在 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info -------- up to this [_image setImage:imageimg];它可以工作,它在 UIImageView 中显示图像,但是 NSLog(@"imgdata:%@",imgdata);没有被调用.... @vaibhav 检查我的回答并告诉我您的反馈。 你想把你的图片作为 NSData 发布到服务器吗? 是的...@DipankarDas 【参考方案1】:

只需配置您的 UIImagePickerController 内部按钮操作,然后尝试。

- (IBAction)changeImage:(id)sender 

   UIImagePickerController *picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;
   picker.allowsEditing = YES;
   picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   [self presentViewController:picker animated:YES completion:NULL];

您可以使用下面的didFinishPickingMediaWithInfo委托方法获取图像数据:

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

   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(chosenImage)];
   NSLog(@"imgdata:%@",imageData);

   self.yourImageView.image = chosenImage;
   [picker dismissViewControllerAnimated:YES completion:NULL];

【讨论】:

比以前更好的东西.....现在我发生了 2 个问题---> 1] 现在它像 imgdata 一样打印: 2] 当我选择图像时它只打印 imgdata:,但是为什么 UIImageView 中没有设置图片??? @SurajSukale 然后您需要将图像设置为 imageView,检查更新的 ans。 是的,我已经尝试过 [_image setImage:chosenImage];这样,现在也使用这个 self.yourImageView.image = selectedImage;但图像没有显示在 UIImageView 中。 imageView 的连接插座一切正常,先生,因为它以前对我有用,只是没有得到 imagedata,现在我明白了,但图像没有显示在 UIImageView 中,而且 imagedata 只是 .. ..?? 实际上问题出在您的didFinishPickingMediaWithInfo 方法中,您在此方法中做了更多的事情,只需从内部删除其他代码并创建差异模块来处理其他任务。

以上是关于为啥我无法正确获取图像数据或无法将数据发送到服务器?的主要内容,如果未能解决你的问题,请参考以下文章

无法将多部分表单数据从 React 正确发送到 Express Js

为啥 PHP 无法从 Js FormData fetch 中获取 $_POST 数据?

无法将数据发送到 recyclerview

在聊天应用程序中使用 Web 套接字 Api 无法正确显示图像

Firebase 存储图像无法正确上传

无法将图像和一个数据上传到服务器