参考:
头像保存:
http://blog.csdn.net/Codingfire/article/details/51943286
http://www.cnblogs.com/linxiu-0925/p/5885638.HTML
头像获取与显示参考
http://blog.csdn.net/CodingFire/article/details/52128773
//把选择的头像保存到本地 func saveImage(image:UIImage)->String{ //修正图片的位置 //下面一句代码报错,暂时没有找到解决方法,以后有时间再弄吧 //let image = fixOrIEntation((info[UIImagePickerControllerOriginalimage] as! UIImage)) //先把图片转成NSData let data = UIImageJPEGRepresentation(image,0.5) //显示图片 //self.imagevIEw.image = UIImage(data: data!) //图片保存的路径 //这里将图片放在沙盒的documents文件夹中 //Home目录 let homeDirectory = NSHomeDirectory() let documentPath = homeDirectory + "/documents" //文件管理器 let fileManager: NSfileManager = NSfileManager.defaultManager() //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png do { try fileManager.createDirectoryAtPath(documentPath,withIntermediateDirectorIEs: true,attributes: nil) } catch _ { } fileManager.createfileAtPath(documentPath.stringByAppendingString("/image.png"),contents: data,attributes: nil) //得到选择后沙盒中图片的完整路径 let filePath: String = String(format: "%@%@",documentPath,"/image.png") // print("filePath:" + filePath) return filePath }
使用方法
//添加代理方法,用于执行图片拾取完成后的代码 func imagePickerController(picker: UIImagePickerController,dIDFinishPickingMediawithInfo info: [String : AnyObject]) { //判断是否允许裁剪 if(picker.allowsEditing){ //裁剪后图片 let image = info["UIImagePickerControllerEditedImage"]as? UIImage // self.imagevIEw.image = image let type: String = (info[UIImagePickerControllerMediaType] as! String) print(type) let imagePath:String = saveImage(image!) //根据保存路径获取并显示图片 let lastData = NSData(contentsOffile: imagePath) self.imagevIEw.image = UIImage(data: lastData!) }else{ //原始图片 let image = info["UIImagePickerControllerOriginalimage"]as? UIImage //self.imagevIEw.image = image let type: String = (info[UIImagePickerControllerMediaType] as! String) print(type) let imagePath:String = saveImage(image!) //根据路径获取图片并显示 let lastData = NSData(contentsOffile: imagePath) self.imagevIEw.image = UIImage(data: lastData!) } self.dismissVIEwControllerAnimated(true,completion: nil ) }总结
以上是内存溢出为你收集整理的swift 头像上传(3)头像保存与读取全部内容,希望文章能够帮你解决swift 头像上传(3)头像保存与读取所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)