gpt4 book ai didi

ios - 从 imagePickerController 选择照片后如何更改标题单元格中的 imageView?

转载 作者:行者123 更新时间:2023-11-30 11:36:52 26 4
gpt4 key购买 nike

我在 Collection View 中有一个自定义标题单元格。下面是我如何从标题中的 url 设置图像

    let profileImage: UIImageView = {
let image = UIImageView()
image.contentMode = .scaleAspectFill
image.layer.cornerRadius = 42
image.layer.masksToBounds = true
return image
}()

var userInfo: CauNguyenUserInfoModel?{
didSet{
if let profileImageUrl = userInfo?.profileImageUrl {
if let imageUrl = URL(string: profileImageUrl) {
profileImage.sd_setImage(with: imageUrl, placeholderImage: #imageLiteral(resourceName: "default_avatar"), options: [.continueInBackground, .progressiveDownload], completed: nil)
}
}
}
}

然后,我使用协议(protocol)和委托(delegate)将操作从 header 类中的按钮传递到 Collection View 类,并使用 UIImagePickController 从用户设备选择不同的图像

这是来 self 的 Collection View Controller 的函数,而不是来自 header 类的函数

 func changeProfileImageBtnPressed() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
present(imagePickerController, animated: true, completion: nil)
}

并使用此函数获取图像文件

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
print(editedImage)
}
dismiss(animated: true, completion: nil)
}

我的问题是,当用户从他的设备中选择图像并且我有图像文件后,如何使用该图像更改 header 类中的图像?我将选择器函数放入我的 Collection View Controller 中。

最佳答案

给你。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
var newImage: UIImage?
if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
newImage = editedImage
} else if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
newImage = originalImage
}
imageView.image = image
picker.dismiss(animated: true)
}

关于ios - 从 imagePickerController 选择照片后如何更改标题单元格中的 imageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49659247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com