当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS将相册中图片上传至服务器的方法

iOS将相册中图片上传至服务器的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论
本文为大家分享了ios图片上传至服务器的具体代码,供大家参考,具体内容如下 在使用app时,从相册中选取图片作为头像是很常用的操作,首先打开相册选择图片,然后将图片保存至

本文为大家分享了ios图片上传至服务器的具体代码,供大家参考,具体内容如下

在使用app时,从相册中选取图片作为头像是很常用的操作,首先打开相册选择图片,然后将图片保存至本应用的document,最后将document中图片的路径保存至nsuserdefaults和服务器。

从相册中选取图片或拍照

//从相册中选取图片或拍照 
- (void)btnactionforeditportrait:(id) sender { 
  uiimagepickercontroller *picker = [[uiimagepickercontroller alloc] init]; 
  picker.delegate = self; 
  picker.sourcetype = uiimagepickercontrollersourcetypephotolibrary; 
  picker.allowsediting = yes; 
  [self presentviewcontroller:picker animated:yes completion:null]; 
} 
 
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { 
   
  _avatar = info[uiimagepickercontrolleroriginalimage]; 
 
  [self saveimage:_avatar withname:@"useravatar"]; 
 
  //处理完毕,回到个人信息页面 
  [picker dismissviewcontrolleranimated:yes completion:null]; 
  [_tableview reloaddata]; 
} 

保存图片 

//保存图片 
- (void)saveimage:(uiimage *)tempimage withname:(nsstring *)imagename 
{ 
  nsdata* imagedata = uiimagepngrepresentation(tempimage); 
  nsstring* documentpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; 
  nsstring* totalpath = [documentpath stringbyappendingpathcomponent:imagename]; 
   
  //保存到 document 
  [imagedata writetofile:totalpath atomically:no]; 
   
  //保存到 nsuserdefaults 
  nsuserdefaults *userdefaults = [nsuserdefaults standarduserdefaults]; 
  [userdefaults setobject:totalpath forkey:@"avatar"]; 
   
  //上传服务器 
  [[hsloginclass new] uploadavatar:totalpath]; 
} 
 
//从document取得图片 
- (uiimage *)getimage:(nsstring *)urlstr 
{ 
  return [uiimage imagewithcontentsoffile:urlstr]; 
}

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网