介绍
编辑用户头像是帐号编辑功能的必备功能之一,现在Swift实现的头像选择还不多,这里给大家简单介绍一下
实现
在viewDidLoad
中为头像添加点击手势响应
UIImageView
默认不支持交互,我们先要允许它交互,需要为它添加一个TapGestureRecognizer
override func viewDidLoad() { super.viewDidLoad() //设置头像圆角 icon.layer.cornerRadius = icon.frame.width/2 //设置遮盖额外部分,下面两句的意义及实现是相同的 //icon.clipsToBounds = true icon.layer.masksToBounds = true //为头像添加点击事件 icon.userInteractionEnabled=true let userIconActionGR = UITapGestureRecognizer() userIconActionGR.addTarget(self, action: Selector("selectIcon")) icon.addGestureRecognizer(userIconActionGR) //从文件读取用户头像 let fullPath = ((NSHomeDirectory() as NSString) .stringByAppendingPathComponent("Documents") as NSString).stringByAppendingPathComponent(iconImageFileName) //可选绑定,若保存过用户头像则显示之 if let savedImage = UIImage(contentsOfFile: fullPath){ self.icon.image = savedImage } }
弹出一个AlertController让用户选择
//选择头像的函数 func selectIcon(){ let userIconAlert = UIAlertController(title: "请选择操作", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet) let chooseFromPhotoAlbum = UIAlertAction(title: "从相册选择", style: UIAlertActionStyle.Default, handler: funcChooseFromPhotoAlbum) userIconAlert.addAction(chooseFromPhotoAlbum) let chooseFromCamera = UIAlertAction(title: "拍照", style: UIAlertActionStyle.Default,handler:funcChooseFromCamera) userIconAlert.addAction(chooseFromCamera) let canelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel,handler: nil) userIconAlert.addAction(canelAction) self.presentViewController(userIconAlert, animated: true, completion: nil) }
根据不同的选择弹出UIImagePickerController
要使用UIImagePickerController
,首先要继承UIImagePickerControllerDelegate
, UINavigationControllerDelegate
//从相册选择照片 func funcChooseFromPhotoAlbum(avc:UIAlertAction) -> Void{ let imagePicker = UIImagePickerController() //设置代理 imagePicker.delegate = self //允许编辑 imagePicker.allowsEditing = true //设置图片源 imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary //模态弹出IamgePickerView self.presentViewController(imagePicker, animated: true, completion: nil) }
//拍摄照片func funcChooseFromCamera(avc:UIAlertAction) -> Void{ let imagePicker = UIImagePickerController() //设置代理 imagePicker.delegate = self //允许编辑 imagePicker.allowsEditing=true //设置图片源 imagePicker.sourceType = UIImagePickerControllerSourceType.Camera //模态弹出IamgePickerView self.presentViewController(imagePicker, animated: true, completion: nil)}
func imagePickerControllerDidCancel(picker: UIImagePickerController){ picker.dismissViewControllerAnimated(true, completion: nil)}
实现UIImagePickerDelegate
方法
//UIImagePicker回调方法 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { //获取照片的原图 //let image = (info as NSDictionary).objectForKey(UIImagePickerControllerOriginalImage) //获得编辑后的图片 let image = (info as NSDictionary).objectForKey(UIImagePickerControllerEditedImage) //保存图片至沙盒 self.saveImage(image as! UIImage, imageName: iconImageFileName) let fullPath = ((NSHomeDirectory() as NSString).stringByAppendingPathComponent("Documents") as NSString).stringByAppendingPathComponent(iconImageFileName) //存储后拿出更新头像 let savedImage = UIImage(contentsOfFile: fullPath) self.icon.image=savedImage picker.dismissViewControllerAnimated(true, completion: nil) }
保存图片到沙盒
//MARK: - 保存图片至沙盒 func saveImage(currentImage:UIImage,imageName:String){ var imageData = NSData() imageData = UIImageJPEGRepresentation(currentImage, 0.5)! // 获取沙盒目录 let fullPath = ((NSHomeDirectory() as NSString).stringByAppendingPathComponent("Documents") as NSString).stringByAppendingPathComponent(imageName) // 将图片写入文件 imageData.writeToFile(fullPath, atomically: false) }
更换效果