您好,欢迎访问一九零五行业门户网

objective-c - Thinkphp+IOS上传图片该怎么处理接收和数据

刚开始接触app端程序处理。
现在问题就卡在tp端接收ios传过来的图片,请问tp端处理上传的图片数据该是怎样流程?要注意些什么问题,如数据格式应该是什么?
请问下面的处理方式正确吗?
ios文件如下:
xlselectview.h文件:
// xlselectview.h// xluploadimages// created by 薛林 on 16/6/18.// copyright © 2016年 xuelin. all rights reserved.#import @class zlphotopickerviewcontroller;@interface xlselectview : uiview//跳转界面的block@property (nonatomic, copy) void(^presentvc)(zlphotopickerviewcontroller *pickervc);//网络需要传入的参数@property (nonatomic, copy) nsstring *posturlstring;//parameters@property (nonatomic, strong) nsdictionary *parameters;//后台接收图片的字段@property (nonatomic, copy) nsstring *userfile;//加载xib+ (instancetype)loadnib;@end

xlselectview.m文件:
// xlselectview.m// xluploadimages//// created by 薛林 on 16/6/18.// copyright © 2016年 xuelin. all rights reserved.//#import xlselectview.h#import zlphoto.h#import afnetworking.h#import dgglobel.h#import dgsecret.h@interface xlselectview ()//保存图片二进制数据@property (nonatomic, strong) nsmutabledictionary *filedict;@end@implementation xlselectview#pragma mark - 懒加载字典- (nsmutabledictionary *)filedict { if (_filedict == nil) { _filedict = [nsmutabledictionary dictionary]; } return _filedict;}#pragma mark - 加载xib+ (instancetype)loadnib { return [[[nsbundle mainbundle]loadnibnamed:@xlselectview owner:nil options:nil]lastobject];}- (ibaction)selectmorepic:(id)sender { // 创建图片多选控制器 zlphotopickerviewcontroller *pickervc = [[zlphotopickerviewcontroller alloc] init]; // 默认显示相册里面的内容savephotos pickervc.status = pickerviewshowstatussavephotos; // 选择图片的最小数,默认是9张图片最大也是9张 pickervc.maxcount = 9; self.presentvc(pickervc); // 用block来回调 __weak typeof(self) weakself = self; pickervc.callback = ^(nsarray *assets){ //遍历获取每一张图片 并转成二进制 for (zlphotoassets *asset in assets) { nsdata *imagedata = uiimagepngrepresentation(asset.originimage); //给图片起随机名字 nsstring *filename = [nsstring stringwithformat:@%d.png,arc4random_uniform(100)]; //保存到filedict中 [weakself.filedict setobject:imagedata forkey:filename]; } };}- (ibaction)oploadpicture:(id)sender { [self original];}- (void)original{ //创建管理者 afhttpsessionmanager *manager = [afhttpsessionmanager manager]; manager.responseserializer = [afhttpresponseserializer serializer]; nsstring *url = @http://www.baidu.cn/index.php/home/index/ugc_tipic; [manager.requestserializer setvalue:@application/json, image/png forhttpheaderfield:@accept];// [manager.requestserializer setvalue:url.absolutestring forhttpheaderfield:@referer]; // 加密 nsstring *mdsecret = [dgsecret md5:[dgglobel getinstance].secret]; nsmutabledictionary *dic = [nsmutabledictionary dictionary]; [dic setvalue:[dgglobel getinstance].user_id forkey:@user_id]; [manager post:url parameters:dic constructingbodywithblock:^(id _nonnull formdata) { [self.filedict enumeratekeysandobjectsusingblock:^(nsstring *savefliename, nsdata *filedata, bool * _nonnull stop) { //获取到每个文件的二进制数据 拼接文本参数 [formdata appendpartwithfiledata:filedata name:@file filename:savefliename mimetype:@image/jpg]; }]; } progress:nil success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { nsdictionary *content = [nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers error:nil]; nslog(@上传成功content = %@,content); } failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { nslog(@上传失败%@,[error localizeddescription]); }];}@end

thinkphp处理文件:
$ugc_topic = m('ugc_topic'); $ugc_image_type = m('ugc_image_type'); $tmp_str = '';$upload = $_post;foreach($upload as $value => $key) { $tmp_str.= '-----'.$key.'=>'.$value.\n\r;}// ----------------调试程序----------------------$filename = dirname(__file__).'/file.txt';$now_time = date('y-m-d h:i:s' , time());$word2 = {$now_time}\n\r{$tmp_str}\n\r\n\r;$fh = fopen($filename, a+);echo fwrite($fh, $word2);fclose($fh);$user_id = i('post.user_id','','htmlspecialchars'); // 当前登陆成功的用户id$title = i('post.title','','htmlspecialchars'); // 当前发布话题的标题$content = i('post.content','','htmlspecialchars'); // 当前发布话题的内容$remind_who = i('post.remind_who','','htmlspecialchars'); // 提醒谁看的用户id$image_name = i('post.image_name','','htmlspecialchars'); // 当前发布话题的图片header(content-type: application/octet-stream);$byte = $_post['image_name'];$byte = str_replace(' ','',$byte); //处理数据 $byte = str_ireplace(,'',$byte);$byte = pack(h*,$byte); //16进制转换成二进制$filename2 = dirname(__file__).'/file.txt';$word22 = \n\r\n\r{$byte}\n\r\n\r;$fhf = fopen($filename2, a+);echo fwrite($fhf, $word22);fclose($fhf);header('content-type: text/json; charset=utf-8');$base64 = $_post[file]; // 得到参数$img = base64_decode($base64); // 将格式为base64的字符串解码$path = md5(uniqid(rand()))..jpg; // 产生随机唯一的名字作为文件名file_put_contents($path, $img); // 将图片保存到相应位置header('content-type: text/json; charset=utf-8' );

请赐教~多谢!
回复内容: 刚开始接触app端程序处理。
现在问题就卡在tp端接收ios传过来的图片,请问tp端处理上传的图片数据该是怎样流程?要注意些什么问题,如数据格式应该是什么?
请问下面的处理方式正确吗?
ios文件如下:
xlselectview.h文件:
// xlselectview.h// xluploadimages// created by 薛林 on 16/6/18.// copyright © 2016年 xuelin. all rights reserved.#import @class zlphotopickerviewcontroller;@interface xlselectview : uiview//跳转界面的block@property (nonatomic, copy) void(^presentvc)(zlphotopickerviewcontroller *pickervc);//网络需要传入的参数@property (nonatomic, copy) nsstring *posturlstring;//parameters@property (nonatomic, strong) nsdictionary *parameters;//后台接收图片的字段@property (nonatomic, copy) nsstring *userfile;//加载xib+ (instancetype)loadnib;@end

xlselectview.m文件:
// xlselectview.m// xluploadimages//// created by 薛林 on 16/6/18.// copyright © 2016年 xuelin. all rights reserved.//#import xlselectview.h#import zlphoto.h#import afnetworking.h#import dgglobel.h#import dgsecret.h@interface xlselectview ()//保存图片二进制数据@property (nonatomic, strong) nsmutabledictionary *filedict;@end@implementation xlselectview#pragma mark - 懒加载字典- (nsmutabledictionary *)filedict { if (_filedict == nil) { _filedict = [nsmutabledictionary dictionary]; } return _filedict;}#pragma mark - 加载xib+ (instancetype)loadnib { return [[[nsbundle mainbundle]loadnibnamed:@xlselectview owner:nil options:nil]lastobject];}- (ibaction)selectmorepic:(id)sender { // 创建图片多选控制器 zlphotopickerviewcontroller *pickervc = [[zlphotopickerviewcontroller alloc] init]; // 默认显示相册里面的内容savephotos pickervc.status = pickerviewshowstatussavephotos; // 选择图片的最小数,默认是9张图片最大也是9张 pickervc.maxcount = 9; self.presentvc(pickervc); // 用block来回调 __weak typeof(self) weakself = self; pickervc.callback = ^(nsarray *assets){ //遍历获取每一张图片 并转成二进制 for (zlphotoassets *asset in assets) { nsdata *imagedata = uiimagepngrepresentation(asset.originimage); //给图片起随机名字 nsstring *filename = [nsstring stringwithformat:@%d.png,arc4random_uniform(100)]; //保存到filedict中 [weakself.filedict setobject:imagedata forkey:filename]; } };}- (ibaction)oploadpicture:(id)sender { [self original];}- (void)original{ //创建管理者 afhttpsessionmanager *manager = [afhttpsessionmanager manager]; manager.responseserializer = [afhttpresponseserializer serializer]; nsstring *url = @http://www.baidu.cn/index.php/home/index/ugc_tipic; [manager.requestserializer setvalue:@application/json, image/png forhttpheaderfield:@accept];// [manager.requestserializer setvalue:url.absolutestring forhttpheaderfield:@referer]; // 加密 nsstring *mdsecret = [dgsecret md5:[dgglobel getinstance].secret]; nsmutabledictionary *dic = [nsmutabledictionary dictionary]; [dic setvalue:[dgglobel getinstance].user_id forkey:@user_id]; [manager post:url parameters:dic constructingbodywithblock:^(id _nonnull formdata) { [self.filedict enumeratekeysandobjectsusingblock:^(nsstring *savefliename, nsdata *filedata, bool * _nonnull stop) { //获取到每个文件的二进制数据 拼接文本参数 [formdata appendpartwithfiledata:filedata name:@file filename:savefliename mimetype:@image/jpg]; }]; } progress:nil success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { nsdictionary *content = [nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers error:nil]; nslog(@上传成功content = %@,content); } failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { nslog(@上传失败%@,[error localizeddescription]); }];}@end

thinkphp处理文件:
$ugc_topic = m('ugc_topic'); $ugc_image_type = m('ugc_image_type'); $tmp_str = '';$upload = $_post;foreach($upload as $value => $key) { $tmp_str.= '-----'.$key.'=>'.$value.\n\r;}// ----------------调试程序----------------------$filename = dirname(__file__).'/file.txt';$now_time = date('y-m-d h:i:s' , time());$word2 = {$now_time}\n\r{$tmp_str}\n\r\n\r;$fh = fopen($filename, a+);echo fwrite($fh, $word2);fclose($fh);$user_id = i('post.user_id','','htmlspecialchars'); // 当前登陆成功的用户id$title = i('post.title','','htmlspecialchars'); // 当前发布话题的标题$content = i('post.content','','htmlspecialchars'); // 当前发布话题的内容$remind_who = i('post.remind_who','','htmlspecialchars'); // 提醒谁看的用户id$image_name = i('post.image_name','','htmlspecialchars'); // 当前发布话题的图片header(content-type: application/octet-stream);$byte = $_post['image_name'];$byte = str_replace(' ','',$byte); //处理数据 $byte = str_ireplace(,'',$byte);$byte = pack(h*,$byte); //16进制转换成二进制$filename2 = dirname(__file__).'/file.txt';$word22 = \n\r\n\r{$byte}\n\r\n\r;$fhf = fopen($filename2, a+);echo fwrite($fhf, $word22);fclose($fhf);header('content-type: text/json; charset=utf-8');$base64 = $_post[file]; // 得到参数$img = base64_decode($base64); // 将格式为base64的字符串解码$path = md5(uniqid(rand()))..jpg; // 产生随机唯一的名字作为文件名file_put_contents($path, $img); // 将图片保存到相应位置header('content-type: text/json; charset=utf-8' );

请赐教~多谢!
其它类似信息

推荐信息