拖入一个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())
}