我无法从UIImageView中的UIImagePickerController加载选定的图像。 UIImageView只需点击collectionViewCell内的按钮即可显示。点击collectionViewCell内的那个按钮,UIImageView可以动画进入视图。在UIImageView上点击了uiimagepicker。选择图像时拾取器会消失,但是在拾取器关闭时,图像不会在uiimageView内部加载(占位符图像保留并且不会被所选图像替换)。我的控制台中没有发生崩溃或错误。谢谢你的帮助!
UICollectionViewCell 类 - LoginCell
class LoginCell: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
lazy var createUsernameButton: UIButton = {
let customButton = UIButton(type: .system)
customButton.setTitle("Create Username", for: .normal)
customButton.addTarget(self, action: #selector(animateIn), for: .touchUpInside)
return customButton
}()
func animateIn() {
addSubview(profileImageView)
profileImageView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
profileImageView.alpha = 0
UIView.animate(withDuration: 0.2) {
self.profileImageView.alpha = 1
self.profileImageView.transform = CGAffineTransform.identity
}
}
lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "avatar-bg-2x")
imageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(handleSelectProfileImage)))
imageView.isUserInteractionEnabled = true
return imageView
}()
var loginController: LoginController?
func handleSelectProfileImage() {
guard let loginController = delegate as? LoginController else {
return
}
loginController.showImagePicker()
}
//.....
}
loginController 类
class LoginController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, LoginControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
weak var loginCollectionView: UICollectionView!
var loginCell: LoginCell?
func showImagePicker() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromImagePicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromImagePicker = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromImagePicker = originalImage
}
dismiss(animated: true, completion: {
if let selectedImage = selectedImageFromImagePicker {
self.loginCell?.profileImageView.image = selectedImage
print("was dismissed")
}
})
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath) as! LoginCell
loginCell.delegate = self
return loginCell
}
// ...
}
答案 0 :(得分:1)
我发现了问题,你没有更新主线程上的图像。试试这个:
1。更改登录控制器类中的代码以匹配以下代码。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromImagePicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromImagePicker = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromImagePicker = originalImage
}
if let selectedImage = selectedImageFromImagePicker {
DispatchQueue.main.async {
self.loginCell?.profileImageView.image = selectedImage
self.loginCollectionView.reloadData() //EDIT: add this new piece of code
}
}
dismiss(animated: true, completion: nil)
}
var profileImage: UIImage?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath) as! LoginCell
loginCell.delegate = self
loginCell.profileImageView.image = profileImage != nil ? profileImage : UIImage(named: "avatar-bg-2x")
return loginCell
}
2. 在loginCell类中更改此代码以匹配以下代码。
lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
//removed line here
imageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(handleSelectProfileImage)))
imageView.isUserInteractionEnabled = true
return imageView
}()