Swift - 从 UICollectionViewCell 获取 UIImage
Swift - get UIImage from UIColllectionViewCell
我有一个 collectionView
,其中每个 cell
包含一个 UIImage
和一个空的 UIButton
。如何显示用户点击的图片?
class ImageCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
@IBOutlet weak var imageCollectionView: UICollectionView!
let images: [UIImage] = [
UIImage(named: "beerImage")!,
UIImage(named: "christmasImage")!,
UIImage(named: "goalImage")!,
UIImage(named: "travelImage")!,
UIImage(named: "rollerImage")!,
UIImage(named: "giftImage")!,
UIImage(named: "shirtImage")!,
UIImage(named: "dressImage")!,
]
let columnLayout = FlowLayout(
itemSize: CGSize(width: 150, height: 150),
minimumInteritemSpacing: 30,
minimumLineSpacing: 10,
sectionInset: UIEdgeInsets(top: 20, left: 20, bottom: 10, right: 20)
)
var colViewWidth: CGFloat = 0.0
override func viewDidLoad() {
self.imageCollectionView.collectionViewLayout = columnLayout
super.viewDidLoad()
imageCollectionView.dataSource = self
imageCollectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = images[indexPath.item]
cell.layer.cornerRadius = 2
return cell
}
@IBAction func imageButtonTapped(_ sender: Any) {
print("tapped")
}
@IBAction func closeButtonTapped(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let HomeViewController = storyBoard.instantiateViewController(withIdentifier: "HomeVC") as! ExampleViewController
self.present(HomeViewController, animated: false, completion: nil)
}
}
更新
最后我想把那个图像传给另一个ViewController。我用这种方法试过,但图片没有显示在另一个 ViewController:
ViewControllerA
var tappedImage = UIImage()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
tappedImage = images[indexPath.row]
}
var showPopUpView = true
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(showPopUpView)
var vc = segue.destination as! ExampleViewController
vc.pickedImage = self.tappedImage
vc.ShowPopUpView = self.showPopUpView
}
ViewControllerB
var pickedImage = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
imagePreview.image = pickedImage
你可以这样做
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
imageToPass = images[indexPath.row]
}
编辑:
至于将该图像传递给下一个视图控制器,一种可能的方法是将 属性 添加到图像的第一个和第二个视图控制器,在 didSelectItemAt
调用中设置该图像,然后传递它使用 prepare(for segue: UIStoryboardSegue, sender: Any?)
let imageToPass: UIImage?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "identifier name here" {
// Get the new view controller using segue.destination.
if let destVC = segue.destination as? SecondViewController {
// Pass the selected object to the new view controller.
destVC.image = imageToPass
}
}
}
试试这个
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let image = images[indexPath.row]
//Result image
}
(或)
如果你想从点击按钮获取图像(不推荐)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = images[indexPath.item]
cell.layer.cornerRadius = 2
//Replace your button
cell.yourButton.tag = indexPath.row
cell.yourButton.addTarget(self, action: #selector(self.imageButtonTapped(_:)), for: .touchUpInside)
}
在按钮操作中
func imageButtonTapped(_ sender: UIButton) {
let image = images[sender.tag]
//Result image
}
由于评论中的对话:
操作顺序应该是:
- 用户点击单元格
- 正在根据所选单元格的
indexPath
获取图片
- 将其设置为临时变量 (
tappedImage
)
- 执行转场
- 将临时图像传递给目的地的临时图像(
pickedImage
)
- 正在加载目的地的视图
- 显示图像。
似乎您缺少其中一个步骤(可能是第 3 步,因为您没有在 ...didSelectItemAt indexPath...
中调用 performSegue
并且它可能根本没有被调用!尝试在那里打印一些东西并检查它是否为真)
或者您可能会扰乱流程(可能您设置图像 在 目的地 View
加载后,结果是看不到它。)
调试:
尝试在这 7 个状态中的每一个上打印您有权访问的所有变量(例如 tappedImage
、pickedImage
、images[indexPath.row]
等),看看问题出在哪里以及您在哪里丢失选定的图像参考
可能的问题
您没有使用 ...didSelectItemAt indexPath...
到 performSegue
,可能您有一个 UIButton
,所以按钮阻止了 cell
被选中。
我有一个 collectionView
,其中每个 cell
包含一个 UIImage
和一个空的 UIButton
。如何显示用户点击的图片?
class ImageCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
@IBOutlet weak var imageCollectionView: UICollectionView!
let images: [UIImage] = [
UIImage(named: "beerImage")!,
UIImage(named: "christmasImage")!,
UIImage(named: "goalImage")!,
UIImage(named: "travelImage")!,
UIImage(named: "rollerImage")!,
UIImage(named: "giftImage")!,
UIImage(named: "shirtImage")!,
UIImage(named: "dressImage")!,
]
let columnLayout = FlowLayout(
itemSize: CGSize(width: 150, height: 150),
minimumInteritemSpacing: 30,
minimumLineSpacing: 10,
sectionInset: UIEdgeInsets(top: 20, left: 20, bottom: 10, right: 20)
)
var colViewWidth: CGFloat = 0.0
override func viewDidLoad() {
self.imageCollectionView.collectionViewLayout = columnLayout
super.viewDidLoad()
imageCollectionView.dataSource = self
imageCollectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = images[indexPath.item]
cell.layer.cornerRadius = 2
return cell
}
@IBAction func imageButtonTapped(_ sender: Any) {
print("tapped")
}
@IBAction func closeButtonTapped(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let HomeViewController = storyBoard.instantiateViewController(withIdentifier: "HomeVC") as! ExampleViewController
self.present(HomeViewController, animated: false, completion: nil)
}
}
更新
最后我想把那个图像传给另一个ViewController。我用这种方法试过,但图片没有显示在另一个 ViewController:
ViewControllerA
var tappedImage = UIImage()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
tappedImage = images[indexPath.row]
}
var showPopUpView = true
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(showPopUpView)
var vc = segue.destination as! ExampleViewController
vc.pickedImage = self.tappedImage
vc.ShowPopUpView = self.showPopUpView
}
ViewControllerB
var pickedImage = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
imagePreview.image = pickedImage
你可以这样做
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
imageToPass = images[indexPath.row]
}
编辑:
至于将该图像传递给下一个视图控制器,一种可能的方法是将 属性 添加到图像的第一个和第二个视图控制器,在 didSelectItemAt
调用中设置该图像,然后传递它使用 prepare(for segue: UIStoryboardSegue, sender: Any?)
let imageToPass: UIImage?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "identifier name here" {
// Get the new view controller using segue.destination.
if let destVC = segue.destination as? SecondViewController {
// Pass the selected object to the new view controller.
destVC.image = imageToPass
}
}
}
试试这个
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let image = images[indexPath.row]
//Result image
}
(或) 如果你想从点击按钮获取图像(不推荐)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = images[indexPath.item]
cell.layer.cornerRadius = 2
//Replace your button
cell.yourButton.tag = indexPath.row
cell.yourButton.addTarget(self, action: #selector(self.imageButtonTapped(_:)), for: .touchUpInside)
}
在按钮操作中
func imageButtonTapped(_ sender: UIButton) {
let image = images[sender.tag]
//Result image
}
由于评论中的对话:
操作顺序应该是:
- 用户点击单元格
- 正在根据所选单元格的
indexPath
获取图片 - 将其设置为临时变量 (
tappedImage
) - 执行转场
- 将临时图像传递给目的地的临时图像(
pickedImage
) - 正在加载目的地的视图
- 显示图像。
似乎您缺少其中一个步骤(可能是第 3 步,因为您没有在 ...didSelectItemAt indexPath...
中调用 performSegue
并且它可能根本没有被调用!尝试在那里打印一些东西并检查它是否为真)
或者您可能会扰乱流程(可能您设置图像 在 目的地 View
加载后,结果是看不到它。)
调试:
尝试在这 7 个状态中的每一个上打印您有权访问的所有变量(例如 tappedImage
、pickedImage
、images[indexPath.row]
等),看看问题出在哪里以及您在哪里丢失选定的图像参考
可能的问题
您没有使用 ...didSelectItemAt indexPath...
到 performSegue
,可能您有一个 UIButton
,所以按钮阻止了 cell
被选中。