利用UIActionsheet完成上传头像

     UIActionsheet的定义和声明我就不写了,主要写功能函数。

此功能函数是主要是从相册中选取图片还是直接调用照相机。

func actionSheet(actionSheet: IBActionSheet!, clickedButtonAtIndex buttonIndex: Int) {

        var sourceType = UIImagePickerControllerSourceType.PhotoLibrary   //默认是从相册中选取图片

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){    //判断手机上的相机是否可用,不可用直接选择默认的从相册中选取图片

            if buttonIndex == 0{

                sourceType = UIImagePickerControllerSourceType.PhotoLibrary     //从相册中选取图片

                //println("从相册中选取")

            }else{

                sourceType = UIImagePickerControllerSourceType.Camera      //调用相机

                //println("拍照")

            }

        }

    // 图片选择器的设置

        imagePickerController.delegate = self    

        imagePickerController.allowsEditing = true

        imagePickerController.sourceType = sourceType

        self.presentViewController(imagePickerController, animated: true, completion: nil)

    }

    //隐藏图片选择器

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {

        self.dismissViewControllerAnimated(true, completion: nil)

    }

    //图片选择器具体功能的实现

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

        var imageToSave:UIImage

        imageToSave = info[UIImagePickerControllerEditedImage] as! UIImage    //选择的图片

        self.portraitImageView.image = imageToSave

        var portraitData = UIImageJPEGRepresentation(imageToSave, 0.1)    //图片压缩

        //下面的代码是指将图片存储到第三方平台LeanCloud上,如果你不用这个可忽略,按自己的存储即可

        var imageFile = AVFile.fileWithName("head.png", data: portraitData) as! AVFile

        imageFile.saveInBackgroundWithBlock({ (success:Bool, error:NSError!) -> Void in

            if error == nil {

            } else {

            }

            }, progressBlock: { (progress:Int) -> Void in

            println(progress)

        })

        currentUser!.setObject(imageFile, forKey: "avatar")


        currentUser!.saveInBackgroundWithBlock { (success:Bool, error:NSError!) -> Void in

            if error == nil{

            

            }else{

                println("why can not")

            }

        }

       //图片选择器隐藏

        self.dismissViewControllerAnimated(true, completion: nil)

    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值