Swift 头像上传(2)

前端之家收集整理的这篇文章主要介绍了Swift 头像上传(2)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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


//从本地获取
    func localImage(){
        
        if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary){
        self.imagePickerController = UIImagePickerController()
        self.imagePickerController.delegate = self
        self.imagePickerController.allowsEditing = true//允许用户裁剪移动缩放
        self.imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary//设置图片来源为图库
        //设置图片拾取器导航条的前景色
        self.imagePickerController.navigationBar.barTintColor = UIColor.orangeColor()
       //设置图片拾取器标题颜色为白色
        self.imagePickerController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
        //设置导航条的着色颜色为白色
        self.imagePickerController.navigationBar.tintColor = UIColor.whiteColor()
   //在当前视图控制器窗口展示图片拾取器
        self.presentViewController(self.imagePickerController,animated: true,completion : nil )
        }else{
        print("读取相册失败")
        }
    }
    
    //打开相机获取
    func openCamera(){
        if UIImagePickerController.isSourceTypeAvailable(.Camera) {
            self.imagePickerController = UIImagePickerController()
            self.imagePickerController.delegate = self
            self.imagePickerController.allowsEditing = true//允许用户裁剪移动缩放
            self.imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera//设置图片来源为相机
            //设置图片拾取器导航条的前景色
            self.imagePickerController.navigationBar.barTintColor = UIColor.orangeColor()
            //设置图片拾取器标题颜色为白色
            self.imagePickerController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
            //设置导航条的着色颜色为白色
            self.imagePickerController.navigationBar.tintColor = UIColor.whiteColor()
            //在当前视图控制器窗口展示图片拾取器
            self.presentViewController(self.imagePickerController,completion : nil )
        
        }else{
        
          print("相机不可用,您可能使用的是模拟器,请切换到真机调试")
        }
    }
    
     //添加代理方法,用于执行图片拾取完成后的代码
    func imagePickerController(picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        //判断是否允许裁剪
        if(picker.allowsEditing){
            //裁剪后图片
            self.imageview.image = info["UIImagePickerControllerEditedImage"]as? UIImage
        }else{
            //原始图片
            self.imageview.image = info["UIImagePickerControllerOriginalImage"]as? UIImage
        }
        
        self.dismissViewControllerAnimated(true,completion: nil )
    
    }
   
    
//添加代理方法,执行用户取消的代码
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        //隐藏图片拾取器
        self.dismissViewControllerAnimated(true,completion: nil )
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


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

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

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

原文链接:https://www.f2er.com/swift/322997.html

猜你在找的Swift相关文章