在这里我是用button来显示头像。所以定义一个avatarBtn,创建一个延迟加载的pickView。然后avatarData是用来保存及接受本地数据的,需要把头像保存到本地的可以用到。
在这里要注意的是在storyboard里面已经设置avatarBtn的形状为圆形了,也可以通过设置avatarBtn的layer.cornerRadius来设置圆角大小
@IBOutlet weak var avatarBtn: UIButton!
private lazy var pickVC: UIImagePickerController = {
let pickVC = UIImagePickerController()
pickVC.delegate = self
pickVC.allowsEditing = true
return pickVC
}()
private var avatarData: NSData?
在viewDidLoad里面先获取本地头像文件,如果有的话就设置图片,其中,imagewithClipImage的作用是设置图片周围有2个像素的白色圆圈。
override func viewDidLoad() {
super.viewDidLoad()
addLeftItemButton()
// Do any additional setup after loading the view.
if let data = FileOP.unarchive(kAvatarFileName) as? NSData {
let image = UIImage.imageWithClipImage(UIImage(data: data)!, borderWidth: 2, borderColor: UIColor.whiteColor())
avatarBtn.setImage(image, forState: .Normal)
}
}
点击头像按钮,响应事件,用alertController来作为选择项
@IBAction func clickAvatarEvent(sender: AnyObject) {
nameField.resignFirstResponder()
let iconActionSheet: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
iconActionSheet.addAction(UIAlertAction(title:"Camera", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) in
self.openCamera()
}))
iconActionSheet.addAction(UIAlertAction(title:"Photo", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) in
self.openPhotoLibrary()
}))
iconActionSheet.addAction(UIAlertAction(title:"Cancel", style: UIAlertActionStyle.Cancel, handler:nil))
self.presentViewController(iconActionSheet, animated: true, completion: nil)
}
然后就是imagePicker的delegate
//MARK:-- imagePicker delegate
extension SSTMyProfileVC: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
//打开相机
private func openCamera() {
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
pickVC.sourceType = .Camera
self.presentViewController(pickVC, animated: true, completion: nil)
} else {
SVProgressHUD.showErrorWithStatus("This camera is invalid!")
}
}
//打开相册
private func openPhotoLibrary() {
pickVC.sourceType = .PhotoLibrary
pickVC.allowsEditing = true
presentViewController(pickVC, animated: true, completion: nil)
}
在这个完成选择的回调里面做的几个操作是把图片缩小,然后保存图片到本地,最后设置avatarBtn的图片
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let typeStr = info[UIImagePickerControllerMediaType] as? String {
if typeStr == "public.image" {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
let smallImage = UIImage.imageClipToNewImage(image, newSize:CGSize(width: 90, height: 90 ))
if UIImagePNGRepresentation(smallImage) == nil {
avatarData = UIImageJPEGRepresentation(smallImage, 0.8)
} else {
avatarData = UIImagePNGRepresentation(smallImage)
}
if avatarData != nil {
//save to local
FileOP.archive(kAvatarFileName, object: avatarData!)
//设置avatar圆角边缘颜色及宽度
let image = UIImage.imageWithClipImage(UIImage(data: avatarData!)!, borderWidth: 2, borderColor: UIColor.whiteColor())
avatarBtn.setImage(image, forState: .Normal)
} else {
SVProgressHUD.showErrorWithStatus("save photo failure")
}
}
}
} else {
SVProgressHUD.showErrorWithStatus("get photo failure")
}
picker.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(true, completion: nil)
}
}