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

时间:2016-12-28 10:26:31

标签: ios objective-c uiimageview

我正在尝试在我的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]);

1 个答案:

答案 0 :(得分: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];
}