Converting UIImage to NSData and NSData to NSString for posting images to a server(将 UIImage 转换为 NSData 并将 NSData 转换为 NSString 以将图像发布到服务器)
问题描述
我有两个 UIImageViews.我想通过脚本将两个图像发布到服务器.在此之前,我必须将这些图像转换为数据并将数据转换为字符串,但我不知道如何将图像从 UIImageView
转换为 NSData
和 NSData
到 NSString
.我已经参考了所有代码,但它不起作用.我也想知道在我的代码中应该在哪里声明转换编码(图像到数据和数据到图像)?
I have two UIImageViews. I want to post two images to server through the script. Before that I have to convert these images to data and data to string but I don't know how to convert the images from UIImageView
to NSData
and NSData
to NSString
.I have referred all the codes but it does not work. I would also like to know as to where should I declare the conversion coding (image to data and data to image) in my code?
这是我已经实现的
.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); }
推荐答案
很久以后我在这里发布我的答案.
After Long time I post my answer here.
选项 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];
方案二:简单字符串编码、解码转换
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);
输出结果是
这篇关于将 UIImage 转换为 NSData 并将 NSData 转换为 NSString 以将图像发布到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 UIImage 转换为 NSData 并将 NSData 转换为 NSString 以将图像发布到服务器
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01