import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let imagePickerController: UIImagePickerController = UIImagePickerController()
var isFullScreen: Bool = false
var imageData=NSData()
var img = UIImage()
@IBOutlet weak var image: UIImageView!
@IBAction func btnClicked(sender: UIButton) {
// 设置代理
self.imagePickerController.delegate = self
// 设置是否可以管理已经存在的图片或者视频
self.imagePickerController.allowsEditing = true
// 判断是否支持相机
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
let alertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let cameraAction: UIAlertAction = UIAlertAction(title: "拍照换头像", style: .Default) { (action: UIAlertAction!) -> Void in
// 设置类型
self.imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(self.imagePickerController, animated: true, completion: nil)
}
alertController.addAction(cameraAction)
let photoLibraryAction: UIAlertAction = UIAlertAction(title: "从相册选择换头像", style: .Default) { (action: UIAlertAction!) -> Void in
// 设置类型
self.imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
//改navigationBar背景色
self.imagePickerController.navigationBar.barTintColor = UIColor(red: 171/255, green: 202/255, blue: 41/255, alpha: 1.0)
//改navigationBar标题色
self.imagePickerController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
//改navigationBar的button字体色
self.imagePickerController.navigationBar.tintColor = UIColor.whiteColor()
self.presentViewController(self.imagePickerController, animated: true, completion: nil)
}
alertController.addAction(photoLibraryAction)
let cancelAction: UIAlertAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil)
}else{
let alertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let photoLibraryAction: UIAlertAction = UIAlertAction(title: "从相册选择换头像", style: .Default) { (action: UIAlertAction!) -> Void in
// 设置类型
self.imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
//改navigationBar背景色
self.imagePickerController.navigationBar.barTintColor = UIColor(red: 171/255, green: 202/255, blue: 41/255, alpha: 1.0)
//改navigationBar标题色
self.imagePickerController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
//改navigationBar的button字体色
self.imagePickerController.navigationBar.tintColor = UIColor.whiteColor()
self.presentViewController(self.imagePickerController, animated: true, completion: nil)
}
alertController.addAction(photoLibraryAction)
let cancelAction: UIAlertAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker.dismissViewControllerAnimated(true, completion: nil)
img = info[UIImagePickerControllerEditedImage] as! UIImage
image.image = img
imageData=UIImageJPEGRepresentation(img, 1)!
//保存文件
var path:Array = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true)
let FilePath=path[0].stringByAppendingString("/HomeImage.png")
imageData.writeToFile(FilePath, atomically: true)
picker.dismissViewControllerAnimated(true, completion: nil)
}
// 当用户取消时,调用该方法
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}