leancloud实现用户头像修改

拖入一个imgeView(hpt)和一个button(imgbutton)

hpt获取用户头像

//获取用户名
let _un = UserDefaults.stand.string(forKey:"name"
//获取用户头像
cql.getHead(userName:_un!,finished:{(img) in
    DispatchQueue.main.async {
        self.hpt.image = img
    }                       
})

定义图片选择器

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        // Dismiss the picker if the user canceled.
        dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        // The info dictionary may contain multiple representations of the image. You want to use the original.
        guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }
        
        // Set photoImageView to display the selected image.
        
        self.hpt.image = selectedImage
        
        // Dismiss the picker.
        dismiss(animated: true, completion: nil)
    }

imgbutton添加事件(需要连接storyboard)

@IBAction func selectImageFromPL(_ sender:UIButton){
    let imagePickerController = UIImagePickController()
    // Only allow photos to be picked, not taken.
    imagePickerController.sourceType = .photoLibrary
    // Make sure ViewController is notified when the user picks an image.
    imagePickerController.delegate = self
    self.present(imagePickerController, animated: true, completion: nil)
}

定义一个更新头像按钮:

@IBAction func commit(_ sender:UIButton){   
    cql.updateHead(userName: username.text!, image: self.hpt.image!.scaleImage(scaleSize: 0.05).toCircle())
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值