Swift 头像上传(2)http://blog.youkuaiyun.com/wei_chong_chong/article/details/52611110

今天接着Swift头像上传(1)继续学习

实现效果:点击相册,打开相册

首先需要实现两个协议UIImagePickerControllerDelegate,UINavigationControllerDelegate实现加载和读取相册的功能


[html]  view plain  copy
 print ?
  1. //从本地获取  
  2.     func localImage(){  
  3.           
  4.         if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary){  
  5.         self.imagePickerController = UIImagePickerController()  
  6.         self.imagePickerController.delegate = self  
  7.         self.imagePickerController.allowsEditing = true//允许用户裁剪移动缩放  
  8.         self.imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary//设置图片来源为图库  
  9.         //设置图片拾取器导航条的前景色  
  10.         self.imagePickerController.navigationBar.barTintColor = UIColor.orangeColor()  
  11.        //设置图片拾取器标题颜色为白色  
  12.         self.imagePickerController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]  
  13.         //设置导航条的着色颜色为白色  
  14.         self.imagePickerController.navigationBar.tintColor = UIColor.whiteColor()  
  15.    //在当前视图控制器窗口展示图片拾取器  
  16.         self.presentViewController(self.imagePickerController, animated: true, completion : nil )  
  17.         }else{  
  18.         print("读取相册失败")  
  19.         }  
  20.     }  
  21.       
  22.     //打开相机获取  
  23.     func openCamera(){  
  24.         if UIImagePickerController.isSourceTypeAvailable(.Camera) {  
  25.             self.imagePickerController = UIImagePickerController()  
  26.             self.imagePickerController.delegate = self  
  27.             self.imagePickerController.allowsEditing = true//允许用户裁剪移动缩放  
  28.             self.imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera//设置图片来源为相机  
  29.             //设置图片拾取器导航条的前景色  
  30.             self.imagePickerController.navigationBar.barTintColor = UIColor.orangeColor()  
  31.             //设置图片拾取器标题颜色为白色  
  32.             self.imagePickerController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]  
  33.             //设置导航条的着色颜色为白色  
  34.             self.imagePickerController.navigationBar.tintColor = UIColor.whiteColor()  
  35.             //在当前视图控制器窗口展示图片拾取器  
  36.             self.presentViewController(self.imagePickerController, animated: true, completion : nil )  
  37.           
  38.         }else{  
  39.           
  40.           print("相机不可用,您可能使用的是模拟器,请切换到真机调试")  
  41.         }  
  42.     }  
  43.       
  44.      //添加代理方法,用于执行图片拾取完成后的代码  
  45.     func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {  
  46.         //判断是否允许裁剪  
  47.         if(picker.allowsEditing){  
  48.             //裁剪后图片  
  49.             self.imageview.image = info["UIImagePickerControllerEditedImage"]as? UIImage  
  50.         }else{  
  51.             //原始图片  
  52.             self.imageview.image = info["UIImagePickerControllerOriginalImage"]as? UIImage  
  53.         }  
  54.           
  55.         self.dismissViewControllerAnimated(true, completion: nil )  
  56.       
  57.     }  
  58.      
  59.       
  60. //添加代理方法,执行用户取消的代码  
  61.     func imagePickerControllerDidCancel(picker: UIImagePickerController) {  
  62.         //隐藏图片拾取器  
  63.         self.dismissViewControllerAnimated(true, completion: nil )  
  64.     }  
  65.   
  66.     override func didReceiveMemoryWarning() {  
  67.         super.didReceiveMemoryWarning()  
  68.         // Dispose of any resources that can be recreated.  
  69.     }  

上面的拍照和本地获取,代码有很多相同的地方,可以优化一下:优化思路,把两个方法合为一个。利用传递参数的方式把获取图片来源传递进来。

把判断相机是否能用,图库是否能打开的条件判断语句放在调用改方法的地方。

下次把优化过的代码贴出来,这次就不贴了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值