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

ios – 将UIImage转换为NSData,将NSData转换为NSString,以便将图像发布到服务器,第1张

概述我有两个UI ImageViews.我想通过脚本将两个图像发布服务器.在此之前,我必须将这些图像转换为数据和数据到字符串,但我不知道如何将图像从UIImageView转换为NSData和NSData转换为NSString.I已经引用了所有代码,但它不起作用.我还想知道在我的代码中应该在哪里声明转换编码(图像到数据和数据到图像)? 这就是我实施的内容 .h部分 #import <UIKit/UIK 我有两个UI ImageVIEws.我想通过脚本将两个图像发布到服务器.在此之前,我必须将这些图像转换为数据和数据到字符串,但我不知道如何将图像从UIImageVIEw转换为NSData和NSData转换为Nsstring.I已经引用了所有代码,但它不起作用.我还想知道在我的代码中应该在哪里声明转换编码(图像到数据和数据到图像)?

这就是我实施的内容

.h部分

#import <UIKit/UIKit.h>  #import "GlobalAccessClass.h"  #import  <QuartzCore/QuartzCore.h>  @interface AskQuestionHome :  UIVIEwController<UITextVIEwDelegate,UITextFIEldDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>  {  }  @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];
总结

以上是内存溢出为你收集整理的ios – 将UIImage转换为NSData,将NSData转换为NSString,以便将图像发布到服务器全部内容,希望文章能够帮你解决ios – 将UIImage转换为NSData,将NSData转换为NSString,以便将图像发布到服务器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1109725.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存