Conversión de UIImage a NSData y NSData a NSString para publicar imágenes en un servidor
Tengo dos UIImageViews. Quiero publicar dos imágenes al servidor a través del script. Antes de eso tengo que convertir estas imágenes en datos y datos en cadenas, pero no sé cómo convertir las imágenes deUIImageView
aNSData
yNSData
aNSString
. He referido todos los códigos pero no funciona. También me gustaría saber dónde debo declarar la codificación de conversión (imagen a datos y datos a imagen) en mi código.
Esto es lo que he implementado.
.h parte
#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 parte
-(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); }