ios - 将 UIImage 转换为 NSData 并将 NSData 转换为 NSString 以将图像发布到服务器

标签 ios iphone uiimageview nsdata

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

这是我实现的

.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 以将图像发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301142/

相关文章:

ios - iOS 7 应用程序中未显示 Mapbox 多边形

iphone - 彩色键盘应用程序(在 App Store 上)如何更改键盘外观?

ios - 在 Apple-Push-Notifications 的应用程序静音

ios - UITabbar 背景图像重复本身

ios - 理解 iBeacon 数据 : the power field and other bytes

ios - 每小时静默推送通知限制

ios - 如何在 swift 2.3 中按升序对数组进行排序

iPhone OpenGL fps 测试

ios - 将 UIImage 设置为自定义 UIButton 时出现问题

ios - 如何识别我点击了哪个 UIImageview?