gpt4 book ai didi

swift - 需要帮助使用 CollectionView 单元格显示从 UIImagePicker 上传到 ImageView 的图像

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

在 Xcode 中构建一个应用程序,我试图将 UIImageView 的图像更改为从 ImagePicker 中选择的图像。我正在处理属于 UICollectionView 中可重用单元格的 ImageView。除了更改实际图像之外,所有代码都有效。

我尝试通过相应的 View Controller 运行代码,尝试将代码添加到 UICollectionViewCell 的类中,并尝试重新加载 Collection View 的数据。

//image picker code
@objc func uploadPhoto(_ sender: UIImageView) {
let uploadPicker = UIImagePickerController()
uploadPicker.delegate = self
present(uploadPicker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImageFromPicker: UIImage?
if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
selectedImageFromPicker = originalImage
print("The photo is \(originalImage)")
}
if let selectedImage = selectedImageFromPicker {
let cell = UploadImageCell()
cell.imageContainer.image = selectedImage
print("Thumbs Up!!")
collectionView.reloadData()
}
dismissView()
}

// CollectionView Code:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: imageCellId, for: indexPath) as! UploadImageCell
let imageOption = ImageOption(rawValue: indexPath.row)
cell.imageContainer.image = imageOption?.icon()
cell.imageContainer.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(cell.uploadPhoto(_:))))
return cell
}
/* other sections use different cells*/
}


//CollectionViewCell Code:

class UploadImageCell: UICollectionViewCell {

// Mark: - Properties

let imageContainer: UIImageView = {
let imageContainer = UIImageView()
imageContainer.clipsToBounds = true
imageContainer.backgroundColor = .blue
imageContainer.isUserInteractionEnabled = true
imageContainer.translatesAutoresizingMaskIntoConstraints = false
return imageContainer
}()

let uploadButton: UIButton = {
let button = UIButton()
button.setTitle("Upload Image", for: .normal)
button.backgroundColor = UIColor.red
button.setTitleColor(.black, for: .normal)
button.layer.cornerRadius = 5
button.layer.shadowColor = UIColor.darkGray.cgColor
button.layer.shadowOffset = CGSize(width: button.frame.width, height: 2)
button.layer.shadowRadius = 5
button.layer.shadowOpacity = 1.0
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()

override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white

addSubview(imageContainer)
NSLayoutConstraint.activate([
imageContainer.topAnchor.constraint(equalTo: topAnchor),
imageContainer.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.9),
imageContainer.widthAnchor.constraint(equalTo: widthAnchor)
])

addSubview(uploadButton)
NSLayoutConstraint.activate([
uploadButton.topAnchor.constraint(equalTo: imageContainer.bottomAnchor, constant: 5),
uploadButton.centerXAnchor.constraint(equalTo: centerXAnchor),
uploadButton.widthAnchor.constraint(equalToConstant: 200),
uploadButton.heightAnchor.constraint(equalToConstant: 30)
])
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

期望 imageContainer.image 的输出为“selectedImage”的值

最佳答案

您需要从 collectionView 加载单元,而不是创建新单元

var selectedCell: UploadImageCell?
@objc func uploadPhoto(_ sender: UIImageView) {
let uploadPicker = UIImagePickerController()
uploadPicker.delegate = self
// Get selected cell
for cell in self.collectionView.visibleCells {
if let uploadCell = cell as? UploadImageCell {
if uploadCell.imageContainer == sender {
selectedCell = uploadCell
}
}
}

present(uploadPicker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImageFromPicker: UIImage?
if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
selectedImageFromPicker = originalImage
print("The photo is \(originalImage)")
}
// Use selectedcell
if let selectedImage = selectedImageFromPicker, let cell = selectedCell {
cell.imageContainer.image = selectedImage
print("Thumbs Up!!")
collectionView.reloadData()
}
dismissView()
}

关于swift - 需要帮助使用 CollectionView 单元格显示从 UIImagePicker 上传到 ImageView 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57796751/

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