博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS小技巧之UIImagePickerController实现头像选择
阅读量:6113 次
发布时间:2019-06-21

本文共 4091 字,大约阅读时间需要 13 分钟。

介绍

编辑用户头像是帐号编辑功能的必备功能之一,现在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)    }

更换效果

图片描述

转载地址:http://pfjka.baihongyu.com/

你可能感兴趣的文章
Scroll Depth – 衡量页面滚动的 Google 分析插件
查看>>
Windows 8.1 应用再出发 - 视图状态的更新
查看>>
自己制作交叉编译工具链
查看>>
Qt Style Sheet实践(四):行文本编辑框QLineEdit及自动补全
查看>>
[物理学与PDEs]第3章习题1 只有一个非零分量的磁场
查看>>
深入浅出NodeJS——数据通信,NET模块运行机制
查看>>
onInterceptTouchEvent和onTouchEvent调用时序
查看>>
android防止内存溢出浅析
查看>>
4.3.3版本之引擎bug
查看>>
SQL Server表分区详解
查看>>
使用FMDB最新v2.3版本教程
查看>>
SSIS从理论到实战,再到应用(3)----SSIS包的变量,约束,常用容器
查看>>
STM32启动过程--启动文件--分析
查看>>
垂死挣扎还是涅槃重生 -- Delphi XE5 公布会归来感想
查看>>
淘宝的几个架构图
查看>>
Android扩展 - 拍照篇(Camera)
查看>>
JAVA数组的定义及用法
查看>>
充分利用HTML标签元素 – 简单的xtyle前端框架
查看>>
设计模式(十一):FACADE外观模式 -- 结构型模式
查看>>
iOS xcodebuile 自动编译打包ipa
查看>>