我正在尝试在我的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]);
最佳答案
只需在按钮操作中配置您的 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
委托(delegate)方法获取图像数据:
- (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];
}
关于ios - 为什么我无法正确获取图像数据或无法将数据发送到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41360010/