leancloudはユーザーのアイコンの変更を実現

1907 ワード

1つのimgeView(hpt)と1つの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())
}