将UIImage转换为NSData,将NSData转换为NSString,以便将图像发布到服务器

我有两个UIImageViews。 我想通过脚本将两个图像发布到服务器。 在此之前,我必须将这些图像转换为数据和数据到字符串,但我不知道如何将图像从UIImageView转换为NSDataNSDataNSString 。我已经引用了所有代码,但它不起作用。 我还想知道在我的代码中应该在哪里声明转换编码(图像到数据和数据到图像)?

这就是我实施的内容

.h部分

  #import  #import "GlobalAccessClass.h" #import  @interface AskQuestionHome : UIViewController { } @property (strong, nonatomic) IBOutlet UIImageView *imgSecondImg; @property (strong, nonatomic) IBOutlet UIImageView *imgFirstImg; @property (strong, nonatomic) IBOutlet UITextView *txtviewAsk; -(IBAction)postbutton:(id)sender; @property (nonatomic,retain) NSString *datestr; @property(nonatomic,retain) NSData *imageData; @property(nonatomic,retain) NSString *postLength; @end 

.m部分

  -(void)post { NSMutableURLRequest *mutableurlrequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://www.alvinchuastudios.com/aNSWERED/insert_question.php"]]; // create the Method "POST" For POSTING the QUESTIION with IMAGES [mutableurlrequest setHTTPMethod:@"POST"]; NSLog(@"the email is:%@",manage.transformEmail); NSLog(@"the cat is:%@",manage.transformCategories); NSLog(@"the text is:%@",textviewText.text); // NSLog(@"the firstimage is:%ld",(long)imgFirstImg.tag); // NSLog(@"the secondimage is:%ld",(long)imgSecondImg.tag); // NSlog(@"the status is:%d",1); NSLog(@"the user is:%@",manage.transformName); NSLog(@"the user registered is:%@",datestr); //passing the string to the server NSString *qususerUpdate =[NSString stringWithFormat:@"email_id=%@&cat=%@&q_text=%@&q_image1=%ld&q_image2=%ld&q_status=1&last_upd_by=%@&last_upd_timestamp=%@",manage.transformEmail,manage.transformCategories,textviewText.text,(long)imgFirstImg.tag,(long)imgSecondImg.tag,manage.transformName,datestr,Nil]; //check the value that what we passed NSLog(@"the data Details is =%@", qususerUpdate); //Convert the String to Data NSData *data1 =[qususerUpdate dataUsingEncoding:NSUTF8StringEncoding]; //Apply the data to the body [mutableurlrequest setHTTPBody:data1]; //Create the response and Error NSError *err; NSURLResponse *response; NSData *responseData =[NSURLConnection sendSynchronousRequest:mutableurlrequest returningResponse:&response error:&err]; NSString *resStr =[[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding]; //This is for Response NSLog(@"got response==%@", resStr); } 

我认为Ashu的回答将返回null。 尝试这个

 NSData *dataImage = [[NSData alloc] init]; dataImage = UIImagePNGRepresentation(image); NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; 
 // From image to data NSData *imgData = [NSData dataWithData:UIImagePNGRepresentation(yourImage)]; // From data to string NSString *string = [[NSString alloc] initWithData:imgData encoding:NSUTF8StringEncoding]; 

答案是对的,在ios中将NSString转换为数据到Uiimage如下:

UIImage * image = nil;

 NSString* path =[NSString stringWithFormat: @"%@",@"http://wx.qlogo.cn/mmopen/q9UTH59ty0K1PRvIQkyydYMia4xN3gib2m2FGh0tiaMZrPS9t4yPJFKedOt5gDFUvM6GusdNGWOJVEqGcSsZjdQGKYm9gr60hibd/0"]; NSURL* url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];//网络图片url NSData* data = [NSData dataWithContentsOfURL:url];//获取网咯图片数据if(data!=nil) { image = [[UIImage alloc] initWithData:data];//根据图片数据流构造image } NSData *dataImage = [[NSData alloc] init]; dataImage = UIImagePNGRepresentation(image); NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; imgstr = stringImage; NSData *data = [[NSData alloc] initWithBase64EncodedString:imgstr options:NSDataBase64DecodingIgnoreUnknownCharacters]; UIImageView *img1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 300, 200, 200)]; img1.image =[[UIImage alloc] initWithData:data];//根据图片数据流构造image [self.view addSubview:img1]; 

很久以后,我在这里发布了答案。

选项1:图像到服务器和图像从服务器

当我们将图像发送到服务器(UIImage-NSData-NSString)时,我们需要使用下面的编码

 NSData *postData = UIImageJPEGRepresentation(myImage, 1.0); NSString *strEncoded = [postData base64EncodedStringWithOptions:0]; 

当我们从服务器(NSString-NSData-UIImage)获取图像时,我们需要使用下面的编码

 NSData *getData = [[NSData alloc] initWithBase64EncodedString:strEncodedFromServer options:0]; UIImage *image = [UIImage imageWithData:getData]; 

选项2:简单字符串编码,解码转换

 NSString *strName = @"iOS"; NSData *dataEncoded = [strName dataUsingEncoding:NSUTF8StringEncoding]; NSString *base64EncodedString = [dataEncoded base64EncodedStringWithOptions:0]; NSLog(@"The Encoded String is - %@", base64EncodedString); NSData *dataDecoded = [[NSData alloc] initWithBase64EncodedString:base64EncodedString options:0]; NSString *strDecoded = [[NSString alloc] initWithData:dataDecoded encoding:NSUTF8StringEncoding]; NSLog(@"The DeCoded String is - %@", strDecoded); 

输出截图是

在此处输入图像描述

选项3:当我们需要对字典进行编码和解码时

 NSDictionary *dictEncoded = @{ @"OS":@"iOS", @"Mobile":@"iPhone", @"Version":@"iOS10" }; NSData *dataDictEncode = [NSJSONSerialization dataWithJSONObject:dictEncoded options:(NSJSONWritingOptions) 0 error:nil]; NSString *strBase64Encode = [dataDictEncode base64EncodedStringWithOptions:0]; NSLog(@"The encoded dictionary is - %@", strBase64Encode); NSData *dataDictDecode = [[NSData alloc] initWithBase64EncodedString:strBase64Encode options:0]; NSDictionary *dictDecoded = [NSJSONSerialization JSONObjectWithData:dataDictDecode options:NSJSONReadingMutableContainers error:nil]; NSLog(@"The decoded dictionary is - %@", dictDecoded); 

输出结果是

在此处输入图像描述

您可以使用以下库来获取前7: https : //github.com/l4u/NSData-Base64/blob/master/NSData%2BBase64.h (或)如果您的应用程序适用于ios 7及更高版本,那么apple已经添加了类别NSData + Base64,它是内置在ios 7 SDK中的

对于使用swift 3的Base64编码

 func createImageString(image: UIImage) -> String? { if let dataImage = UIImagePNGRepresentation(image) { return dataImage.base64EncodedString(options: .lineLength64Characters) } return nil }